Skip to content

Commit

Permalink
Re-enable sitemap
Browse files Browse the repository at this point in the history
Closes #620
  • Loading branch information
maiertech committed Feb 25, 2023
1 parent 15ac7cb commit 30c191d
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/routes/[topic]/+page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] })
});
Expand Down
54 changes: 54 additions & 0 deletions src/routes/sitemap.xml/+server.js
Original file line number Diff line number Diff line change
@@ -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 `
<url>
<loc>${new URL(path, ORIGIN).href}</loc>
${lastmod ? `<lastmod>${lastmod}</lastmod>` : ''}
</url>
`;
}

/** @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 = `
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${pages.join('\n')}
</urlset>
`;

return new Response(sitemap, {
headers: {
'Content-Type': 'application/xml'
}
});
}
36 changes: 0 additions & 36 deletions src/routes/sitemap.xml/server.ts

This file was deleted.

7 changes: 7 additions & 0 deletions thunder-tests/thunderCollection.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
14 changes: 14 additions & 0 deletions thunder-tests/thunderclient.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
}
]

0 comments on commit 30c191d

Please sign in to comment.