|
1 | | -import { existsSync, mkdirSync, copyFileSync } from 'node:fs'; |
| 1 | +import { existsSync, mkdirSync, copyFileSync, rmSync, writeFileSync } from 'node:fs'; |
2 | 2 | import { join } from 'node:path'; |
3 | 3 |
|
4 | | -// Renders the /404 route output to dist/.../browser/404.html so Cloudflare |
5 | | -// Pages serves it for any unknown URL with a real HTTP 404 status. |
6 | | - |
7 | 4 | const browser = 'dist/SchulyWebsite/browser'; |
8 | | -const src = join(browser, '404', 'index.html'); |
9 | | -const dest = join(browser, '404.html'); |
10 | 5 |
|
11 | | -if (!existsSync(src)) { |
12 | | - console.warn(`[postbuild] skipped: ${src} not found.`); |
| 6 | +const ORIGIN = 'https://schuly.dev'; |
| 7 | +const LANGS = ['en', 'de', 'fr', 'it', 'rm'] as const; |
| 8 | +const DEFAULT_LANG = 'en'; |
| 9 | + |
| 10 | +const PAGES: { path: string; changefreq: string; priority: string; images?: string[] }[] = [ |
| 11 | + { |
| 12 | + path: '', |
| 13 | + changefreq: 'weekly', |
| 14 | + priority: '1.0', |
| 15 | + images: [ |
| 16 | + 'assets/app_icon.png', |
| 17 | + 'assets/schuly-start-page.png', |
| 18 | + 'assets/schuly-agenda-page.png', |
| 19 | + 'assets/schuly-grades-page.png', |
| 20 | + 'assets/schuly-absences-page.png', |
| 21 | + ], |
| 22 | + }, |
| 23 | + { path: '/impressum', changefreq: 'yearly', priority: '0.3' }, |
| 24 | + { path: '/privacy', changefreq: 'monthly', priority: '0.5' }, |
| 25 | + { path: '/terms', changefreq: 'monthly', priority: '0.4' }, |
| 26 | +]; |
| 27 | + |
| 28 | +// 1. Render the /404 route output to 404.html so Cloudflare Pages serves it for |
| 29 | +// any unknown URL with a real HTTP 404 status. |
| 30 | +function writeNotFound() { |
| 31 | + const src = join(browser, '404', 'index.html'); |
| 32 | + const dest = join(browser, '404.html'); |
| 33 | + if (!existsSync(src)) { |
| 34 | + console.warn(`[postbuild] 404 skipped: ${src} not found.`); |
| 35 | + return; |
| 36 | + } |
| 37 | + mkdirSync(browser, { recursive: true }); |
| 38 | + copyFileSync(src, dest); |
| 39 | + console.log(`[postbuild] wrote ${dest}`); |
| 40 | +} |
| 41 | + |
| 42 | +// 2. The root '' route only redirects to /<default-lang>. Drop any prerendered |
| 43 | +// root index.html so Cloudflare's `/ -> /en` edge redirect in _redirects |
| 44 | +// applies instead of serving a static shell. |
| 45 | +function dropRootIndex() { |
| 46 | + const root = join(browser, 'index.html'); |
| 47 | + if (existsSync(root)) { |
| 48 | + rmSync(root); |
| 49 | + console.log(`[postbuild] removed ${root} (root handled by _redirects)`); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// 3. Generate a multilingual sitemap with hreflang alternates for every page. |
| 54 | +function writeSitemap() { |
| 55 | + const lastmod = new Date().toISOString().slice(0, 10); |
| 56 | + |
| 57 | + const urls = LANGS.flatMap(lang => |
| 58 | + PAGES.map(page => { |
| 59 | + const loc = `${ORIGIN}/${lang}${page.path}`; |
| 60 | + const alternates = [ |
| 61 | + ...LANGS.map(l => ` <xhtml:link rel="alternate" hreflang="${l}" href="${ORIGIN}/${l}${page.path}"/>`), |
| 62 | + ` <xhtml:link rel="alternate" hreflang="x-default" href="${ORIGIN}/${DEFAULT_LANG}${page.path}"/>`, |
| 63 | + ].join('\n'); |
| 64 | + const images = (page.images ?? []) |
| 65 | + .map(img => ` <image:image>\n <image:loc>${ORIGIN}/${img}</image:loc>\n </image:image>`) |
| 66 | + .join('\n'); |
| 67 | + return [ |
| 68 | + ' <url>', |
| 69 | + ` <loc>${loc}</loc>`, |
| 70 | + alternates, |
| 71 | + ` <lastmod>${lastmod}</lastmod>`, |
| 72 | + ` <changefreq>${page.changefreq}</changefreq>`, |
| 73 | + ` <priority>${page.priority}</priority>`, |
| 74 | + images, |
| 75 | + ' </url>', |
| 76 | + ] |
| 77 | + .filter(Boolean) |
| 78 | + .join('\n'); |
| 79 | + }), |
| 80 | + ).join('\n'); |
| 81 | + |
| 82 | + const xml = `<?xml version="1.0" encoding="UTF-8"?> |
| 83 | +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" |
| 84 | + xmlns:xhtml="http://www.w3.org/1999/xhtml" |
| 85 | + xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"> |
| 86 | +${urls} |
| 87 | +</urlset> |
| 88 | +`; |
| 89 | + |
| 90 | + writeFileSync(join(browser, 'sitemap.xml'), xml); |
| 91 | + console.log(`[postbuild] wrote ${join(browser, 'sitemap.xml')} (${LANGS.length * PAGES.length} urls)`); |
| 92 | +} |
| 93 | + |
| 94 | +if (!existsSync(browser)) { |
| 95 | + console.warn(`[postbuild] skipped: ${browser} not found.`); |
13 | 96 | process.exit(0); |
14 | 97 | } |
15 | 98 |
|
16 | | -mkdirSync(browser, { recursive: true }); |
17 | | -copyFileSync(src, dest); |
18 | | -console.log(`[postbuild] wrote ${dest}`); |
| 99 | +writeNotFound(); |
| 100 | +dropRootIndex(); |
| 101 | +writeSitemap(); |
0 commit comments