From 95381a82097ec7fb784093144bd256043939998d Mon Sep 17 00:00:00 2001 From: Tanner Heffner Date: Fri, 12 Jan 2024 23:30:33 -0800 Subject: [PATCH] add sitemap --- src/lib/siteConfig.js | 3 +- src/routes/(main)/+page.svelte | 1 - src/routes/rss.xml/+server.js | 18 +++++--- src/routes/sitemap.xml/+server.js | 72 +++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 src/routes/sitemap.xml/+server.js diff --git a/src/lib/siteConfig.js b/src/lib/siteConfig.js index 6baa41c..7f561be 100644 --- a/src/lib/siteConfig.js +++ b/src/lib/siteConfig.js @@ -1,4 +1,5 @@ -export const SITE_URL = 'https://heffner.netlify.app'; +// export const SITE_URL = 'https://heffner.netlify.app'; +export const SITE_URL = 'https://heffner.dev'; export const GH_USER = 'tjheffner'; export const GH_USER_REPO = 'tjheffner/heffdotdev'; // used for pulling github issues and offering comments export const SITE_TITLE = 'heffner.dev'; diff --git a/src/routes/(main)/+page.svelte b/src/routes/(main)/+page.svelte index 4705d06..6b87ba8 100644 --- a/src/routes/(main)/+page.svelte +++ b/src/routes/(main)/+page.svelte @@ -8,7 +8,6 @@ DEFAULT_OG_IMAGE, MY_TWITTER_HANDLE } from '$lib/siteConfig'; - export const prerender = false; // index page is most visited, lets prerender diff --git a/src/routes/rss.xml/+server.js b/src/routes/rss.xml/+server.js index 171bdb6..64b8bbb 100644 --- a/src/routes/rss.xml/+server.js +++ b/src/routes/rss.xml/+server.js @@ -1,29 +1,37 @@ import RSS from 'rss'; import { SITE_TITLE, SITE_URL } from '$lib/siteConfig'; +import { remark } from 'remark'; +import remarkHTML from 'remark-html'; import { listContent } from '$lib/content'; // Reference: https://github.com/sveltejs/kit/blob/master/examples/hn.svelte.dev/src/routes/%5Blist%5D/rss.js /** @type {import('@sveltejs/kit').RequestHandler} */ -export async function GET() { +export async function GET({ fetch }) { const feed = new RSS({ title: SITE_TITLE + ' RSS Feed', site_url: SITE_URL, - feed_url: SITE_URL + '/rss.xml' + feed_url: SITE_URL + '/api/rss.xml' }); - const allBlogs = await listContent(); + const allBlogs = await listContent(fetch); allBlogs.forEach((post) => { + // extract HTML from markdown + const htmlDescription = remark() + .use(remarkHTML) + .processSync(post.description) + feed.item({ title: post.title, url: SITE_URL + `/${post.slug}`, date: post.date, - description: post.description + description: htmlDescription.toString() }); }); + // Suggestion (check for correctness before using): return new Response(feed.xml({ indent: true }), { headers: { - 'Cache-Control': `max-age=0, s-maxage=${600}`, // 10 minutes + 'Cache-Control': `public, max-age=${86400}`, // 24 hours 'Content-Type': 'application/rss+xml' } }); diff --git a/src/routes/sitemap.xml/+server.js b/src/routes/sitemap.xml/+server.js new file mode 100644 index 0000000..508b2ae --- /dev/null +++ b/src/routes/sitemap.xml/+server.js @@ -0,0 +1,72 @@ +import { SITE_URL } from '$lib/siteConfig'; +import { listContent } from '$lib/content'; +import { fetchMarkdownPosts } from '$lib/localContent' + +/** @type {import('@sveltejs/kit').RequestHandler} */ +export async function GET({ fetch }) { + const posts = await listContent(fetch); + const projects = await fetchMarkdownPosts() + const pages = [`about`, 'resume', 'blogroll', 'christmas', 'blog', 'work']; + const body = sitemap(posts, projects, pages); + + return new Response(body, { + headers: { + 'Cache-Control': `public, max-age=${86400}`, // 24 hours + 'Content-Type': 'application/xml' + } + }); +} + +const sitemap = (posts, projects, pages) => ` + + + ${SITE_URL} + daily + 1.0 + + ${pages + .map( + (page) => ` + + ${SITE_URL}/${page} + monthly + 0.9 + + ` + ) + .join('')} + ${posts + .map((post) => + post.isPrivate + ? null + : ` + + ${SITE_URL}/${post.slug} + daily + 0.7 + ${post.ghMetadata.updated_at ? post.ghMetadata.updated_at : post.ghMetadata.created_at} + + ` + ) + .join('')} + ${projects + .map((project) => + project.isPrivate + ? null + : ` + + ${SITE_URL}/work/${project.slug} + monthly + 0.8 + + ` + ) + .join('')} + `;