From 6def6e8d8f51cba1583751adfd68338437d76698 Mon Sep 17 00:00:00 2001 From: matthieu42morin Date: Wed, 8 Nov 2023 23:58:51 +0100 Subject: [PATCH] rss 2 alternatives, TODO: CHOOSE --- src/routes/blog/rss.xml/+server.ts | 53 ++++++++++++++++++++++++++ src/routes/blog/rss.xml/alternative.ts | 37 ++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/routes/blog/rss.xml/+server.ts create mode 100644 src/routes/blog/rss.xml/alternative.ts diff --git a/src/routes/blog/rss.xml/+server.ts b/src/routes/blog/rss.xml/+server.ts new file mode 100644 index 0000000..2dfe5de --- /dev/null +++ b/src/routes/blog/rss.xml/+server.ts @@ -0,0 +1,53 @@ +import { listBlogPosts } from '$lib/contents/blog'; +import RSS from 'rss'; + +export const GET = async () => { + const posts = listBlogPosts(); + + /* + The RSS feed is a JavaScript object that contains information about the blog feed. + It has a title, description, copyright, and other properties. + It also has an array of categories that can be used to filter the blog feed. + The pubDate property indicates when the feed was last updated. + */ + + const feed = new RSS({ + title: "Matt's C4vern", + description: "Matt's personal blog about ventures in tech.", + copyright: `Copyright © ${new Date().getFullYear()} Matt Morin. All rights reserved`, + ttl: 1800, + feed_url: 'https://www.mattmor.in/blog', + site_url: 'https://www.mattmor.in', + image_url: 'https://www.mattmor.in/favicon192.png', + language: 'en', + categories: ["Matt's C4vern updates", 'Tech', 'Disruption', 'DevOps', 'Ventures'], + pubDate: new Date().toUTCString(), + generator: 'Matt Morin' + }); + + // This code creates an RSS feed. It does so by iterating over all posts and + // adding each post to the feed. + + posts.forEach((post: any) => { + feed.item({ + title: post.title, + description: post.excerpt, + url: `https://www.mattmor.in/blog/${post.slug}`, + guid: `https://www.mattmor.in/blog/${post.slug}`, + categories: post.tags, + date: post.date, + enclosure: { + url: `https://www.mattmor.in/images/blog/${post.slug}/${post.image}`, + type: 'image/webp' + }, + author: post.author + }); + }); + + return new Response(feed.xml(), { + headers: { + 'Cache-Control': 'max-age=0, s-max-age=3600', + 'Content-Type': 'application/xml' + } + }); +}; diff --git a/src/routes/blog/rss.xml/alternative.ts b/src/routes/blog/rss.xml/alternative.ts new file mode 100644 index 0000000..d66ee04 --- /dev/null +++ b/src/routes/blog/rss.xml/alternative.ts @@ -0,0 +1,37 @@ +import * as config from '$lib/config'; +import type { BlogPost } from '$lib/types'; + +export const prerender = true; + +export async function GET({ fetch }) { + const response = await fetch('api/posts'); + const posts: BlogPost[] = await response.json(); + + const headers = { 'Content-Type': 'application/xml' }; + + const xml = ` + + + ${config.title} + ${config.description} + ${config.url} + + ${posts + .map( + (post) => ` + + ${post.title} + ${post.description} + ${config.url}/${post.slug} + ${config.url}/${post.slug} + ${new Date(post.date).toUTCString()} + + ` + ) + .join('')} + + + `.trim(); + + return new Response(xml, { headers }); +}