Skip to content

Commit 74a0eb2

Browse files
committed
feat(Sitemap): add HOSTNAME env var to generate a sitemap
Fix #476
1 parent f8f0302 commit 74a0eb2

4 files changed

Lines changed: 64 additions & 13 deletions

File tree

.github/workflows/sharevb-vercel-prod.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ env:
44
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
55
VITE_VERCEL_ENV: production
66
VITE_VERCEL_DEPLOY: true
7+
HOSTNAME: https://sharevb-it-tools.vercel.app/
78
on:
89
push:
910
branches:
@@ -20,4 +21,4 @@ jobs:
2021
- name: Build Project Artifacts
2122
run: vercel build --prod --token=${{ secrets.VERCEL_TOKEN }}
2223
- name: Deploy Project Artifacts to Vercel
23-
run: vercel deploy --prebuilt --archive=tgz --prod --token=${{ secrets.VERCEL_TOKEN }}
24+
run: vercel deploy --prebuilt --archive=tgz --prod --token=${{ secrets.VERCEL_TOKEN }}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@
379379
"utf8": "^3.0.0",
380380
"uuid": "^14.0.1",
381381
"vanilla-jsoneditor": "^3.12.0",
382+
"vite-plugin-sitemap": "^0.8.2",
382383
"vue": "^3.5.39",
383384
"vue-color-wheel": "^0.1.1",
384385
"vue-i18n": "^11.4.6",
@@ -457,6 +458,7 @@
457458
"commander": "^15.0.0",
458459
"consola": "^3.4.2",
459460
"dotenv": "^17.4.2",
461+
"fast-glob": "^3.3.3",
460462
"hygen": "^6.2.11",
461463
"jsdom": "^29.1.1",
462464
"less": "^4.6.7",

pnpm-lock.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vite.config.ts

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ import Icons from 'unplugin-icons/vite';
1818
import IconsResolver from 'unplugin-icons/resolver';
1919
import VueI18n from '@intlify/unplugin-vue-i18n/vite';
2020

21+
import fs from 'node:fs';
22+
import fg from 'fast-glob';
23+
import Sitemap from 'vite-plugin-sitemap';
24+
2125
import { visualizer } from 'rollup-plugin-visualizer';
2226

2327
const baseUrl = process.env.BASE_URL || '/';
28+
const hostname = process.env.HOSTNAME;
2429

2530
// Locales are code-split: only en is bundled eagerly, the rest become lazy chunks fetched on
2631
// first use (see src/plugins/i18n.plugin.ts). VITE_AVAILABLE_LOCALES filters the locales
2732
// offered at runtime instead of trimming the build.
28-
const includeLocales = [
29-
resolve(__dirname, 'src/tools/*/locales/**'),
30-
resolve(__dirname, 'locales/**'),
31-
];
33+
const includeLocales = [resolve(__dirname, 'src/tools/*/locales/**'), resolve(__dirname, 'locales/**')];
3234

3335
// https://vitejs.dev/config/
3436
export default defineConfig({
@@ -67,15 +69,16 @@ export default defineConfig({
6769
// tool chunk and WASM binary (~160 MB) on first visit; hashed assets are
6870
// cached on demand as tools are opened. Set VITE_PWA_FULL_PRECACHE=true to
6971
// restore full offline precaching of everything.
70-
globPatterns: (process.env.VITE_PWA_FULL_PRECACHE === 'true' && !process.env.VITE_VERCEL_DEPLOY)
71-
? ['**\/*.{js,wasm,css,html}']
72-
: ['**\/*.{css,html}'],
72+
globPatterns:
73+
process.env.VITE_PWA_FULL_PRECACHE === 'true' && !process.env.VITE_VERCEL_DEPLOY
74+
? ['**\/*.{js,wasm,css,html}']
75+
: ['**\/*.{css,html}'],
7376
maximumFileSizeToCacheInBytes: 25 * 1024 ** 2,
7477
navigateFallback: `${baseUrl}index.html`,
7578
runtimeCaching: [
7679
{
77-
urlPattern: ({ sameOrigin, request }) => sameOrigin
78-
&& (request.destination === 'script' || request.destination === 'worker'),
80+
urlPattern: ({ sameOrigin, request }) =>
81+
sameOrigin && (request.destination === 'script' || request.destination === 'worker'),
7982
handler: 'CacheFirst',
8083
options: {
8184
cacheName: 'app-chunks',
@@ -142,19 +145,44 @@ export default defineConfig({
142145
nodePolyfills(),
143146
wasm(),
144147
visualizer(),
148+
hostname
149+
? Sitemap({
150+
hostname,
151+
generateRobotsTxt: true,
152+
robots: [{ userAgent: '*', allow: '/' }],
153+
dynamicRoutes: (() => {
154+
const paths = ['/', '/about'];
155+
fg.sync('src/tools/*/index.ts').forEach((file) => {
156+
const content = fs.readFileSync(file, 'utf-8');
157+
const pathMatch = content.match(/path:\s*['"`]([^'"`]+)['"`]/);
158+
if (pathMatch) {
159+
paths.push(pathMatch[1]);
160+
}
161+
const redirectMatch = content.match(/redirectFrom:\s*\[([^\]]+)\]/);
162+
if (redirectMatch?.[1]) {
163+
const redirectPaths = redirectMatch[1].match(/['"`]([^'"`]+)['"`]/g);
164+
redirectPaths?.forEach((p) => paths.push(p.replace(/['"`]/g, '')));
165+
}
166+
});
167+
return paths;
168+
})(),
169+
})
170+
: undefined,
145171
],
146172
base: baseUrl,
147173
resolve: {
148174
alias: {
149175
'@': fileURLToPath(new URL('./src', import.meta.url)),
150176
'node:fs/promises': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
151177
'node:fs': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
152-
'fs': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
178+
fs: fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
153179
'@babel/core': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
154180
'isolated-vm': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
155181
'onnxruntime-node': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
156182
'unpdf/pdfjs': fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
157-
'webcrypto-liner-shim': !process.env.VERCEL ? 'webcrypto-liner-shim' : fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
183+
'webcrypto-liner-shim': !process.env.VERCEL
184+
? 'webcrypto-liner-shim'
185+
: fileURLToPath(new URL('./src/_empty.ts', import.meta.url)),
158186
},
159187
},
160188
define: {
@@ -197,7 +225,16 @@ export default defineConfig({
197225
},
198226
},
199227
optimizeDeps: {
200-
include: ['isolated-vm', '@lezer/highlight', 'pdfjs-dist', 'onnxruntime-node', 'onnxruntime-web', 'unpdf', 'unpdf/pdfjs', ...(process.env.VERCEL ? ['webcrypto-liner-shim'] : [])], // optionally specify dependency name
228+
include: [
229+
'isolated-vm',
230+
'@lezer/highlight',
231+
'pdfjs-dist',
232+
'onnxruntime-node',
233+
'onnxruntime-web',
234+
'unpdf',
235+
'unpdf/pdfjs',
236+
...(process.env.VERCEL ? ['webcrypto-liner-shim'] : []),
237+
], // optionally specify dependency name
201238
},
202239
// server: {
203240
// headers: {

0 commit comments

Comments
 (0)