Skip to content

Commit

Permalink
add sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
tjheffner committed Jan 13, 2024
1 parent 6f87f35 commit 95381a8
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/lib/siteConfig.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
1 change: 0 additions & 1 deletion src/routes/(main)/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
DEFAULT_OG_IMAGE,
MY_TWITTER_HANDLE
} from '$lib/siteConfig';
export const prerender = false; // index page is most visited, lets prerender
</script>

<svelte:head>
Expand Down
18 changes: 13 additions & 5 deletions src/routes/rss.xml/+server.js
Original file line number Diff line number Diff line change
@@ -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'
}
});
Expand Down
72 changes: 72 additions & 0 deletions src/routes/sitemap.xml/+server.js
Original file line number Diff line number Diff line change
@@ -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) => `<?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>1.0</priority>
</url>
${pages
.map(
(page) => `
<url>
<loc>${SITE_URL}/${page}</loc>
<changefreq>monthly</changefreq>
<priority>0.9</priority>
</url>
`
)
.join('')}
${posts
.map((post) =>
post.isPrivate
? null
: `
<url>
<loc>${SITE_URL}/${post.slug}</loc>
<changefreq>daily</changefreq>
<priority>0.7</priority>
<lastmod>${post.ghMetadata.updated_at ? post.ghMetadata.updated_at : post.ghMetadata.created_at}</lastmod>
</url>
`
)
.join('')}
${projects
.map((project) =>
project.isPrivate
? null
: `
<url>
<loc>${SITE_URL}/work/${project.slug}</loc>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
`
)
.join('')}
</urlset>`;

0 comments on commit 95381a8

Please sign in to comment.