37 lines
1.5 KiB
JavaScript
37 lines
1.5 KiB
JavaScript
// Scans src/app for public static pages and writes src/lib/sitemap-config.generated.json.
|
|
// Runs on `prebuild`; served by GET /meshkee/sitemap-config.json.
|
|
import { readdirSync, statSync, writeFileSync } from "node:fs";
|
|
import { join, relative, sep } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const root = fileURLToPath(new URL("..", import.meta.url));
|
|
const appDir = join(root, "src", "app");
|
|
const domain = process.env.NEXT_PUBLIC_WEBSITE_DOMAIN || "homgroup.com";
|
|
|
|
const EXCLUDE = /^(api|meshkee|login|register|cart|checkout|account|admin)(\/|$)/;
|
|
const PRIORITY = { "/": 1.0, "/about": 0.6, "/contact": 0.5 };
|
|
|
|
function walk(dir, out = []) {
|
|
for (const entry of readdirSync(dir)) {
|
|
const full = join(dir, entry);
|
|
if (statSync(full).isDirectory()) walk(full, out);
|
|
else if (entry === "page.tsx" || entry === "page.ts") out.push(full);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
const staticPages = walk(appDir)
|
|
.map((file) => relative(appDir, file).split(sep).slice(0, -1).join("/"))
|
|
.filter((route) => !route.includes("[") && !EXCLUDE.test(route))
|
|
.map((route) => `/${route}`.replace(/\/+/g, "/"))
|
|
.sort((a, b) => (a === "/" ? -1 : b === "/" ? 1 : a.localeCompare(b)))
|
|
.map((path) => ({
|
|
path,
|
|
...(path === "/" ? { changefreq: "weekly" } : {}),
|
|
priority: PRIORITY[path] ?? 0.5,
|
|
}));
|
|
|
|
const config = { baseUrl: `https://${domain}`, staticPages };
|
|
writeFileSync(join(root, "src", "lib", "sitemap-config.generated.json"), `${JSON.stringify(config, null, 2)}\n`);
|
|
console.log(`sitemap-config: ${staticPages.map((page) => page.path).join(", ")}`);
|