Files
web/apps/scandic-web/app/sitemap/route.ts
Linus Flood 68763ee1fd Merged in fix/sitemap-fixes (pull request #2280)
fix:sitemap - added null check and correct content-type

* fix:sitemap - added null check and correct content-type

* Revert test code


Approved-by: Erik Tiekstra
2025-06-03 14:48:24 +00:00

39 lines
1.0 KiB
TypeScript

import { notFound } from "next/navigation"
import { env } from "@/env/server"
import { getLastUpdated, getSitemapIds } from "@/utils/sitemap"
export const dynamic = "force-dynamic"
export async function GET() {
if (env.NEW_SITE_LIVE_STATUS !== "ALL_LANGUAGES_LIVE") {
return notFound()
}
console.log(`[SITEMAP] Fetching sitemap`)
const lastUpdated = await getLastUpdated()
const sitemaps = await getSitemapIds()
if (!sitemaps || sitemaps.length === 0) {
return new Response("No sitemaps found", { status: 404 })
}
console.log(`[SITEMAP] Sitemaps retrieved: ${sitemaps.length}`)
const urls = sitemaps.map(
(id) => `<sitemap>
<loc>${env.PUBLIC_URL}/sitemap-${id}.xml</loc>
<lastmod>${lastUpdated}</lastmod>
</sitemap>`
)
const sitemapIndexXML = `<?xml version="1.0" encoding="UTF-8"?>\n<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${urls.join("")}\n</sitemapindex>`
return new Response(sitemapIndexXML, {
headers: { "Content-Type": "application/xml" },
})
}