SPB Git

Repository Public

route.ts 1.6 KB 21 lines typescript

1import { env } from "@/lib/env";2import { allPublishedForFeeds, listTopics } from "@/server/sheets";34export const dynamic = "force-dynamic";5const esc = (s: string) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");67/** Published sheets only, with hreflang alternates between the language versions of the same sheet. */8export async function GET() {9  const [sheets, topics] = await Promise.all([allPublishedForFeeds(), listTopics("fr")]);10  const groups = new Map<string, typeof sheets[number][]>();11  for (const s of sheets) groups.set(s.localization_id, [...(groups.get(s.localization_id) ?? []), s]);12  const url = (loc: string, lastmod?: Date, alts: { locale: string; href: string }[] = []) =>13    `<url><loc>${esc(loc)}</loc>${lastmod ? `<lastmod>${lastmod.toISOString()}</lastmod>` : ""}${alts.map((a) => `<xhtml:link rel="alternate" hreflang="${a.locale}" href="${esc(a.href)}"/>`).join("")}</url>`;14  const body = [15    url(env.appUrl + "/"), url(env.appUrl + "/topics"), url(env.appUrl + "/about"),16    ...topics.filter((t) => t.count > 0).map((t) => url(`${env.appUrl}/topics/${t.slug}`)),17    ...sheets.map((s) => { const g = groups.get(s.localization_id)!; return url(`${env.appUrl}/s/${s.slug}`, s.published_at, g.length > 1 ? g.map((x) => ({ locale: x.locale, href: `${env.appUrl}/s/${x.slug}` })) : []); }),18  ].join("");19  return new Response(`<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">${body}</urlset>`, { headers: { "Content-Type": "application/xml; charset=utf-8", "Cache-Control": "public, max-age=600" } });20}21
Files main
Menu

Theme