From fc74ca4f3e858cbff7fbae3e51908bc7841e9f98 Mon Sep 17 00:00:00 2001
From: Bart van der Braak <bartvdbraak@gmail.com>
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[]) => `<?xml version="1.0" encoding="UTF-8" ?>
+  <urlset
+    xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"
+    xmlns:news="https://www.google.com/schemas/sitemap-news/0.9"
+    xmlns:xhtml="https://www.w3.org/1999/xhtml"
+    xmlns:mobile="https://www.google.com/schemas/sitemap-mobile/1.0"
+    xmlns:image="https://www.google.com/schemas/sitemap-image/1.1"
+    xmlns:video="https://www.google.com/schemas/sitemap-video/1.1"
+  >
+    <url>
+      <loc>${SITE_URL}</loc>
+      <changefreq>daily</changefreq>
+      <priority>0.7</priority>
+    </url>
+    ${pages
+			.map(
+				(page) => `
+    <url>
+      <loc>${SITE_URL}/${page}</loc>
+      <changefreq>daily</changefreq>
+      <priority>0.7</priority>
+    </url>
+    `
+			)
+			.join('')}
+  </urlset>`;