From 73030b22db022b598fbde80bb53c03763c8d4fab Mon Sep 17 00:00:00 2001 From: luen Date: Wed, 4 Nov 2026 12:53:47 +1000 Subject: [PATCH] Add a dynamic sitemap.xml for the main site pages. Lists NAV routes plus season and nursery profiles, and points robots.txt at the production sitemap URL (#140). Co-authored-by: Cursor --- src/routes/sitemap.xml/+server.ts | 60 +++++++++++++++++++++++++++++++ static/robots.txt | 2 ++ 2 files changed, 62 insertions(+) create mode 100644 src/routes/sitemap.xml/+server.ts diff --git a/src/routes/sitemap.xml/+server.ts b/src/routes/sitemap.xml/+server.ts new file mode 100644 index 0000000..9ae7f9a --- /dev/null +++ b/src/routes/sitemap.xml/+server.ts @@ -0,0 +1,60 @@ +import { NAV } from '#lib/nav'; +import { activeNurseries } from '#lib/server/analytics/common'; +import { dataset } from '#lib/server/data'; +import type { RequestHandler } from './$types'; + +const DEFAULT_ORIGIN = 'https://data.physioshark.org'; + +/** Prefer the public site URL so reverse-proxy localhost origins never leak into the sitemap. */ +function siteOrigin(url: URL): string { + for (const raw of [process.env.PUBLIC_SITE_URL, process.env.ORIGIN]) { + if (!raw) continue; + try { + return new URL(raw).origin; + } catch { + /* ignore invalid */ + } + } + if (url.hostname === 'data.physioshark.org' || url.hostname === 'physioshark.org') { + return url.origin; + } + return DEFAULT_ORIGIN; +} + +function escapeXml(s: string): string { + return s + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"'); +} + +export const GET: RequestHandler = ({ url }) => { + const origin = siteOrigin(url); + const d = dataset(); + const lastmod = d.meta.imported_at?.slice(0, 10) || undefined; + + const paths = new Set(NAV.flatMap((g) => g.items.map((i) => i.href))); + for (const season of d.seasons) paths.add(`/seasons/${encodeURIComponent(season)}`); + for (const id of activeNurseries(d)) paths.add(`/nurseries/${encodeURIComponent(id)}`); + + const urls = [...paths] + .sort((a, b) => a.localeCompare(b)) + .map((path) => { + const loc = `${origin}${path === '/' ? '/' : path}`; + const last = lastmod ? `\n\t\t${lastmod}` : ''; + return `\t\n\t\t${escapeXml(loc)}${last}\n\t`; + }) + .join('\n'); + + const body = + `\n` + + `\n${urls}\n\n`; + + return new Response(body, { + headers: { + 'content-type': 'application/xml; charset=utf-8', + 'cache-control': 'public, max-age=3600' + } + }); +}; diff --git a/static/robots.txt b/static/robots.txt index c2a49f4..eca4f58 100644 --- a/static/robots.txt +++ b/static/robots.txt @@ -1,2 +1,4 @@ User-agent: * Allow: / + +Sitemap: https://data.physioshark.org/sitemap.xml