Skip to content

Commit

Permalink
feat: rss.xml feed implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
maehr committed Jan 29, 2024
1 parent 9d1652a commit e42ad77
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/routes/rss.xml/+server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as config from '$lib/config';

/**
* Indicates whether prerendering is enabled.
* @type {boolean}
*/
export const prerender = true;

/** @type {import('./$types').RequestHandler} */
export async function GET({ url, fetch }) {
const posts = await fetch('/posts.json').then((res) => res.json());
const pages = await fetch('/pages.json').then((res) => res.json());
const staticPages = ['agenda', 'archiv', 'blog', 'credits', 'partner'];

let rss = '<?xml version="1.0" encoding="UTF-8"?>';
rss += '<rss version="2.0">';
rss += '<channel>';
rss += `<title>${config.title}</title>`;
rss += `<link>${url.origin}</link>`;
rss += `<description>${config.description}</description>`;
rss += `<language>${config.lang}</language>`;

posts.forEach((post) => {
rss += `<item>`;
rss += `<title>${post.title}</title>`;
rss += `<link>${url.origin}/blog/${post.slug}</link>`;
rss += `<guid>${url.origin}/blog/${post.slug}</guid>`;
rss += `<pubDate>${new Date(post.date).toUTCString()}</pubDate>`;
rss += `<lastBuildDate>${new Date(post.modified).toUTCString()}</lastBuildDate>`;
rss += `<description>${post.excerpt}</description>`;
rss += `</item>`;
});

pages.forEach((page) => {
rss += `<item>`;
rss += `<title>${page.title}</title>`;
rss += `<link>${url.origin}/${page.slug}</link>`;
rss += `<guid>${url.origin}/${page.slug}</guid>`;
rss += `<pubDate>${new Date(page.date).toUTCString()}</pubDate>`;
rss += `<lastBuildDate>${new Date(page.modified).toUTCString()}</lastBuildDate>`;
rss += `<description>${page.excerpt}</description>`;
rss += `</item>`;
});

staticPages.forEach((page) => {
rss += `<item>`;
rss += `<title>${page}</title>`;
rss += `<link>${url.origin}/${page}</link>`;
rss += `<guid>${url.origin}/${page}</guid>`;
rss += `<pubDate>${new Date().toUTCString()}</pubDate>`;
rss += `<lastBuildDate>${new Date().toUTCString()}</lastBuildDate>`;
rss += `<description>${page}</description>`;
rss += `</item>`;
});

rss += `</channel>`;
rss += `</rss>`;

return new Response(rss, {
headers: {
'Content-Type': 'application/rss+xml; charset=utf-8'
}
});
}

0 comments on commit e42ad77

Please sign in to comment.