From 30c191d5a2496072bd7a7f8c2eac10359b4e03ec Mon Sep 17 00:00:00 2001 From: Thilo Maier Date: Sat, 25 Feb 2023 20:00:29 +0000 Subject: [PATCH] Re-enable sitemap Closes #620 --- .env.example | 3 ++ .gitpod.yml | 1 + src/routes/[topic]/+page.js | 2 +- src/routes/sitemap.xml/+server.js | 54 ++++++++++++++++++++++++++++ src/routes/sitemap.xml/server.ts | 36 ------------------- thunder-tests/thunderCollection.json | 7 ++++ thunder-tests/thunderclient.json | 14 ++++++++ 7 files changed, 80 insertions(+), 37 deletions(-) create mode 100644 src/routes/sitemap.xml/+server.js delete mode 100644 src/routes/sitemap.xml/server.ts diff --git a/.env.example b/.env.example index e4c996e4..47ea22d2 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,8 @@ # You need the following environment variables for development, preview and production. +# /sitemap.xml +ORIGIN=https://maier.tech + # EmailOctopus API. EO_API_KEY=api_key EO_LIST_ID=list_id diff --git a/.gitpod.yml b/.gitpod.yml index 8e9e7712..68b4f992 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -18,6 +18,7 @@ vscode: extensions: - dbaeumer.vscode-eslint - esbenp.prettier-vscode + - mattpocock.ts-error-translator - rangav.vscode-thunder-client - robole.marky-stats - svelte.svelte-vscode diff --git a/src/routes/[topic]/+page.js b/src/routes/[topic]/+page.js index 2ebef888..dfb2305a 100644 --- a/src/routes/[topic]/+page.js +++ b/src/routes/[topic]/+page.js @@ -24,7 +24,7 @@ export async function load({ fetch, params }) { const [resolved_topic] = result_topic.data; // Fetch posts for topic. - response = await fetch('/api/posts', { + response = await fetch('/', { method: 'POST', body: JSON.stringify({ topics: [resolved_topic.id] }) }); diff --git a/src/routes/sitemap.xml/+server.js b/src/routes/sitemap.xml/+server.js new file mode 100644 index 00000000..a7202000 --- /dev/null +++ b/src/routes/sitemap.xml/+server.js @@ -0,0 +1,54 @@ +import { ORIGIN } from '$env/static/private'; +import { error } from '@sveltejs/kit'; +import { PostsSchema } from '$lib/schemas/content'; + +/** + * Create page string for sitemap. + * @param {string} path + * @param {string} [lastmod] + */ +function createEntry(path, lastmod) { + return ` + + ${new URL(path, ORIGIN).href} + ${lastmod ? `${lastmod}` : ''} + + `; +} + +/** @type {import('./$types').RequestHandler} */ +export async function GET({ fetch }) { + // Create entries for posts. + const response = await fetch('/api/posts', { + method: 'POST', + body: JSON.stringify({}) + }); + + if (!response.ok) { + throw error(500, 'Failed to fetch posts.'); + } + + const result = PostsSchema.safeParse(await response.json()); + + if (!result.success) { + throw error(500, 'Posts failed validation.'); + } + + const posts = result.data.map((post) => createEntry(`/posts/${post.slug}`, post.modified)); + + // Add additional entries to this array. + const pages = [...posts]; + + const sitemap = ` + + + ${pages.join('\n')} + + `; + + return new Response(sitemap, { + headers: { + 'Content-Type': 'application/xml' + } + }); +} diff --git a/src/routes/sitemap.xml/server.ts b/src/routes/sitemap.xml/server.ts deleted file mode 100644 index 0cf99b43..00000000 --- a/src/routes/sitemap.xml/server.ts +++ /dev/null @@ -1,36 +0,0 @@ -import tags from '$lib/data/tags'; -import topics from '$lib/data/topics'; -import { BASE_URL } from '$lib/data/site'; -import posts from '$lib/data/posts'; - -// Generate a basic XML sitemap. -// See https://developers.google.com/search/docs/advanced/sitemaps/build-sitemap#xml. - -function createPage(href: string, lastmod?: string | Date) { - return ` - - ${new URL(href, BASE_URL).href} - ${lastmod ? '' + lastmod + '' : ''} - - `; -} - -export const GET = async function () { - const postPages = posts.map((post) => createPage(post.slug, post.modified)); - const topicPages = topics.map((topic) => createPage(topic.path)); - const tagPages = tags.map((tag) => createPage(tag.path)); - - const pages = [createPage('/'), createPage('posts'), ...postPages, ...topicPages, ...tagPages]; - - const sitemap = ` - - ${pages.join('\n')} - - `; - - return new Response(sitemap, { - headers: { - 'Content-Type': 'application/xml' - } - }); -}; diff --git a/thunder-tests/thunderCollection.json b/thunder-tests/thunderCollection.json index 78a59edc..8d7198db 100644 --- a/thunder-tests/thunderCollection.json +++ b/thunder-tests/thunderCollection.json @@ -84,6 +84,13 @@ "containerId": "", "created": "2023-02-23T13:17:42.230Z", "sortNum": 11250 + }, + { + "_id": "1434763f-9562-43d5-8b28-f0d100d75e3b", + "name": "/sitemap.xml", + "containerId": "", + "created": "2023-02-25T17:00:20.755Z", + "sortNum": 60000 } ], "settings": { diff --git a/thunder-tests/thunderclient.json b/thunder-tests/thunderclient.json index a7e89737..11a7a402 100644 --- a/thunder-tests/thunderclient.json +++ b/thunder-tests/thunderclient.json @@ -531,5 +531,19 @@ "form": [] }, "tests": [] + }, + { + "_id": "66cd67f0-e86d-44e1-9873-197edee875b1", + "colId": "cc563a09-5210-4394-a84e-085efcca6dab", + "containerId": "1434763f-9562-43d5-8b28-f0d100d75e3b", + "name": "success", + "url": "http://127.0.0.1:5173/sitemap.xml", + "method": "GET", + "sortNum": 110000, + "created": "2023-02-25T17:00:30.328Z", + "modified": "2023-02-25T17:00:54.585Z", + "headers": [], + "params": [], + "tests": [] } ]