Skip to content

Commit

Permalink
update sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedriad1 committed Dec 12, 2024
1 parent 68a2770 commit 3ab0e2b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/app/sitemap.xml/route.ts
Original file line number Diff line number Diff line change
@@ -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 `<sitemap><loc>${getLoc(id)}</loc><lastmod>${getLastMod()}</lastmod></sitemap>`;
}

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 = `<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${getSitemaps(sitemapIds as any)}
</sitemapindex>
`;

return new Response(xml, {
headers: {
"Content-Type": "application/xml",
},
});
}
File renamed without changes.

0 comments on commit 3ab0e2b

Please sign in to comment.