|
| 1 | +/** |
| 2 | + * Writes `robots.txt` and `sitemap.xml` into `dist/` after the export. |
| 3 | + * |
| 4 | + * The sitemap routes come from the exported HTML files, so the sitemap cannot |
| 5 | + * drift from the app. `+not-found` is the 404 page and stays out. |
| 6 | + * |
| 7 | + * This site is the origin root of rootnative.github.io, and a crawler reads |
| 8 | + * `robots.txt` at the root only. So this file lists every sitemap of the |
| 9 | + * origin: this page, and the docs and demo sites the other repositories |
| 10 | + * deploy under their own paths. A new docs site adds one line here. |
| 11 | + */ |
| 12 | +import { existsSync, readdirSync } from 'node:fs' |
| 13 | +import { dirname, resolve } from 'node:path' |
| 14 | +import { fileURLToPath } from 'node:url' |
| 15 | +import { writeRobots, writeSitemap } from '@rootnative/seo/node' |
| 16 | + |
| 17 | +const here = dirname(fileURLToPath(import.meta.url)) |
| 18 | +const dist = resolve(here, '..', 'dist') |
| 19 | + |
| 20 | +const SITE_URL = 'https://rootnative.github.io' |
| 21 | +const EXCLUDED = new Set(['+not-found.html', '404.html', '_sitemap.html']) |
| 22 | + |
| 23 | +const SITEMAPS = [ |
| 24 | + `${SITE_URL}/sitemap.xml`, |
| 25 | + `${SITE_URL}/ui/sitemap.xml`, |
| 26 | + `${SITE_URL}/ui/demo/sitemap.xml`, |
| 27 | + `${SITE_URL}/inertia/sitemap.xml`, |
| 28 | + `${SITE_URL}/impulse/sitemap.xml`, |
| 29 | +] |
| 30 | + |
| 31 | +if (!existsSync(dist)) { |
| 32 | + console.error('[write-seo] dist not found — run `yarn export:web` first.') |
| 33 | + process.exit(1) |
| 34 | +} |
| 35 | + |
| 36 | +const urls = readdirSync(dist) |
| 37 | + .filter((file) => file.endsWith('.html') && !EXCLUDED.has(file)) |
| 38 | + .sort() |
| 39 | + .map((file) => { |
| 40 | + const route = file === 'index.html' ? '/' : `/${file.replace(/\.html$/, '')}` |
| 41 | + return { loc: route, changefreq: 'weekly', priority: route === '/' ? 1 : 0.7 } |
| 42 | + }) |
| 43 | + |
| 44 | +const sitemap = await writeSitemap({ outDir: dist, siteUrl: SITE_URL, urls }) |
| 45 | +const robots = await writeRobots({ outDir: dist, sitemapUrl: SITEMAPS }) |
| 46 | + |
| 47 | +console.log(`[write-seo] ${urls.length} routes → ${sitemap}`) |
| 48 | +console.log(`[write-seo] ${robots} (${SITEMAPS.length} sitemaps)`) |
0 commit comments