From ea3409b50f6dd9508510c3237160ac572a3288a8 Mon Sep 17 00:00:00 2001 From: Bart van der Braak Date: Mon, 14 Aug 2023 11:54:21 +0200 Subject: [PATCH] feat: create dynamic sitemap --- src/routes/sitemap.xml/+server.ts | 41 +++++++++++++++++++++++++++++++ 1 file changed, 41 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..0d4e24f --- /dev/null +++ b/src/routes/sitemap.xml/+server.ts @@ -0,0 +1,41 @@ +import { SITE_URL } from '$lib/site-config'; + +/** @type {import('@sveltejs/kit').RequestHandler} */ +export async function GET() { + const pages = ['projects', 'toolbox']; + const body = sitemap(pages); + + return new Response(body, { + headers: { + 'Cache-Control': `public, max-age=${86400}`, // 24 hours + 'Content-Type': 'application/xml' + } + }); +} + +const sitemap = (pages: string[]) => ` + + + ${SITE_URL} + daily + 0.7 + + ${pages + .map( + (page) => ` + + ${SITE_URL}/${page} + daily + 0.7 + + ` + ) + .join('')} + `;