feat: generate sitemap.xml at build time via PUBLIC_URL env var
Add a Vite plugin that writes a sitemap.xml containing all static routes to the build output. Enabled by setting the PUBLIC_URL environment variable (e.g. PUBLIC_URL=https://ditto.pub) in .env or the shell. When PUBLIC_URL is not set, no sitemap is generated. Also fix a pre-existing LSP type error caused by importing Plugin from 'vite' while defineConfig came from 'vitest/config' (which bundles a different vite version). Now both come from 'vite'. Add explicit 'import process' for node:process.
This commit is contained in:
Generated
-25
@@ -3159,7 +3159,6 @@
|
||||
"cpu": [
|
||||
"arm"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3173,7 +3172,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3187,7 +3185,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3201,7 +3198,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3215,7 +3211,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3229,7 +3224,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3243,7 +3237,6 @@
|
||||
"cpu": [
|
||||
"arm"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3257,7 +3250,6 @@
|
||||
"cpu": [
|
||||
"arm"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3271,7 +3263,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3285,7 +3276,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3299,7 +3289,6 @@
|
||||
"cpu": [
|
||||
"loong64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3313,7 +3302,6 @@
|
||||
"cpu": [
|
||||
"loong64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3327,7 +3315,6 @@
|
||||
"cpu": [
|
||||
"ppc64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3341,7 +3328,6 @@
|
||||
"cpu": [
|
||||
"ppc64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3355,7 +3341,6 @@
|
||||
"cpu": [
|
||||
"riscv64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3369,7 +3354,6 @@
|
||||
"cpu": [
|
||||
"riscv64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3383,7 +3367,6 @@
|
||||
"cpu": [
|
||||
"s390x"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3397,7 +3380,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3411,7 +3393,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3425,7 +3406,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3439,7 +3419,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3453,7 +3432,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3467,7 +3445,6 @@
|
||||
"cpu": [
|
||||
"ia32"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3481,7 +3458,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -3495,7 +3471,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
|
||||
+82
-4
@@ -1,10 +1,10 @@
|
||||
import process from "node:process";
|
||||
import { execSync } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
import react from "@vitejs/plugin-react";
|
||||
import type { Plugin } from "vite";
|
||||
import { defineConfig } from "vitest/config";
|
||||
import { defineConfig, loadEnv, type Plugin } from "vite";
|
||||
|
||||
import { DittoConfigSchema } from "./src/lib/schemas";
|
||||
|
||||
@@ -92,6 +92,78 @@ function mergePublicDir(externalDir: string): Plugin {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* All static routes in the application (no dynamic params).
|
||||
* Used to generate a sitemap.xml at build time.
|
||||
*/
|
||||
const staticRoutes = [
|
||||
'/',
|
||||
'/feed',
|
||||
'/notifications',
|
||||
'/search',
|
||||
'/trends',
|
||||
'/settings',
|
||||
'/settings/profile',
|
||||
'/settings/feed',
|
||||
'/settings/content',
|
||||
'/settings/wallet',
|
||||
'/settings/notifications',
|
||||
'/settings/advanced',
|
||||
'/settings/magic',
|
||||
'/settings/network',
|
||||
'/lists',
|
||||
'/events',
|
||||
'/photos',
|
||||
'/videos',
|
||||
'/vines',
|
||||
'/music',
|
||||
'/podcasts',
|
||||
'/polls',
|
||||
'/treasures',
|
||||
'/colors',
|
||||
'/packs',
|
||||
'/webxdc',
|
||||
'/articles',
|
||||
'/decks',
|
||||
'/emojis',
|
||||
'/development',
|
||||
'/themes',
|
||||
'/bookmarks',
|
||||
'/ai-chat',
|
||||
'/world',
|
||||
'/badges',
|
||||
'/books',
|
||||
'/help',
|
||||
];
|
||||
|
||||
/**
|
||||
* Vite plugin that generates a sitemap.xml in the build output.
|
||||
* Set the PUBLIC_URL env var (e.g. "https://ditto.pub") to enable.
|
||||
* Skipped when PUBLIC_URL is not set.
|
||||
*/
|
||||
function generateSitemap(origin: string): Plugin {
|
||||
return {
|
||||
name: 'ditto:sitemap',
|
||||
writeBundle(options) {
|
||||
const outDir = options.dir ?? path.resolve('dist');
|
||||
|
||||
const urls = staticRoutes
|
||||
.map((route) => ` <url>\n <loc>${new URL(route, origin).href}</loc>\n </url>`)
|
||||
.join('\n');
|
||||
|
||||
const xml = [
|
||||
'<?xml version="1.0" encoding="UTF-8"?>',
|
||||
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
|
||||
urls,
|
||||
'</urlset>',
|
||||
'',
|
||||
].join('\n');
|
||||
|
||||
fs.writeFileSync(path.join(outDir, 'sitemap.xml'), xml);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const dittoConfig = loadDittoConfig();
|
||||
const publicDir = process.env.PUBLIC_DIR;
|
||||
|
||||
@@ -106,7 +178,11 @@ function getVersion(): string {
|
||||
|
||||
|
||||
// https://vitejs.dev/config/
|
||||
export default defineConfig(() => ({
|
||||
export default defineConfig(({ mode }) => {
|
||||
const env = loadEnv(mode, process.cwd(), '');
|
||||
const publicUrl = env.PUBLIC_URL;
|
||||
|
||||
return {
|
||||
server: {
|
||||
host: "::",
|
||||
port: 8080,
|
||||
@@ -114,6 +190,7 @@ export default defineConfig(() => ({
|
||||
plugins: [
|
||||
react(),
|
||||
...(publicDir ? [mergePublicDir(publicDir)] : []),
|
||||
...(publicUrl ? [generateSitemap(publicUrl)] : []),
|
||||
],
|
||||
define: {
|
||||
__DITTO_CONFIG__: JSON.stringify(dittoConfig ?? null),
|
||||
@@ -138,4 +215,5 @@ export default defineConfig(() => ({
|
||||
"@": path.resolve(__dirname, "./src"),
|
||||
},
|
||||
},
|
||||
}));
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user