diff --git a/src/app/sitemap.xml/route.ts b/src/app/sitemap.xml/route.ts new file mode 100644 index 0000000..22ea369 --- /dev/null +++ b/src/app/sitemap.xml/route.ts @@ -0,0 +1,41 @@ +import { relativeUrl } from "@/lib/sitemap"; +import { generateSitemaps } from "../sitemap/sitemap"; +import { env } from "@/env"; + +export const revalidate = 60 * 60 * 24 * 7; // cache for 7 days + +function getLoc(id: number) { + return relativeUrl(`/sitemap/${id}.xml`); +} + +function getLastMod() { + return new Date().toISOString(); +} + +function getSitemap(id: number) { + return `${getLoc(id)}${getLastMod()}`; +} + +function getSitemaps(ids: { id: number }[]) { + return ids.map(({ id }) => getSitemap(id)).join(""); +} + +export async function GET() { + const sitemapIds = generateSitemaps(); + + // We only want to generate the sitemap.xml file in production + const isProd = env.VERCEL_ENV === "production"; + if (!isProd) return Response.json({ error: "Not found" }, { status: 404 }); + + const xml = ` + + ${getSitemaps(sitemapIds as any)} + + `; + + return new Response(xml, { + headers: { + "Content-Type": "application/xml", + }, + }); +} diff --git a/src/app/sitemap.ts b/src/app/sitemap/sitemap.ts similarity index 100% rename from src/app/sitemap.ts rename to src/app/sitemap/sitemap.ts