diff --git a/.gitignore b/.gitignore index 64f20e4..b3f4ba9 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,6 @@ yarn-error.log* # typescript *.tsbuildinfo next-env.d.ts + +# generated by scripts/generate-sitemap.mjs +/public/utilities/sitemap.xml diff --git a/package.json b/package.json index cf09058..524681c 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "private": true, "scripts": { "dev": "next dev --webpack", - "build": "next build --webpack", + "sitemap": "node scripts/generate-sitemap.mjs", + "build": "npm run sitemap && next build --webpack", "pages:build": "next-on-pages", "start": "next start", "lint": "eslint .", diff --git a/scripts/generate-sitemap.mjs b/scripts/generate-sitemap.mjs new file mode 100644 index 0000000..93e2350 --- /dev/null +++ b/scripts/generate-sitemap.mjs @@ -0,0 +1,33 @@ +// Writes public/utilities/sitemap.xml with one entry per page in pages/utilities. +// The site is served at https://jam.dev/utilities, so that is where the file lands. +import { mkdirSync, readdirSync, writeFileSync } from "node:fs"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +const SITE_URL = "https://jam.dev"; + +const root = path.join(path.dirname(fileURLToPath(import.meta.url)), ".."); +const pagesDir = path.join(root, "pages", "utilities"); +const outputFile = path.join(root, "public", "utilities", "sitemap.xml"); + +const urls = readdirSync(pagesDir) + .filter((file) => file.endsWith(".tsx")) + .map((file) => file.replace(/\.tsx$/, "")) + .sort() + .map((page) => { + if (page === "index") { + return `${SITE_URL}/utilities`; + } + return `${SITE_URL}/utilities/${page}`; + }); + +const sitemap = [ + '', + '', + ...urls.map((url) => ` ${url}`), + "", + "", +].join("\n"); + +mkdirSync(path.dirname(outputFile), { recursive: true }); +writeFileSync(outputFile, sitemap);