An RSS feed for a Fumadocs blog
How to add an RSS feed to a blog built with Fumadocs, the two ways to write it, how readers find it, and how to follow this one.
Audrey KlammerFumadocsNext.js
RSS is still the simplest way to follow a site: no account, no algorithm, just new posts in the reader you already use. A Fumadocs blog has everything a feed needs, because every post is already typed data with a title, a date and a description.
To follow this blog, add https://01orbital.dev/blog/rss.xml to your feed
reader. Most readers also find it if you paste in https://01orbital.dev/blog.
The rest of this post is how to build one for your own site.
Fumadocs' way: the feed package
Fumadocs has a short guide,
RSS Feed. It uses the
feed package to write the XML, and a
route handler to serve it:
npm install feed
import { Feed } from "feed"
import { blogSource } from "@/lib/blog"
const baseUrl = "https://your-site.com"
export function getRSS() {
const feed = new Feed({
title: "Your blog",
id: `${baseUrl}/blog`,
link: `${baseUrl}/blog`,
language: "en",
copyright: "All rights reserved",
})
for (const post of blogSource.getPages()) {
feed.addItem({
id: post.url,
title: post.data.title,
description: post.data.description,
link: `${baseUrl}${post.url}`,
date: post.data.date,
})
}
return feed.rss2()
}import { getRSS } from "@/lib/rss"
export const revalidate = false
export function GET() {
return new Response(getRSS())
}Two changes from the guide's example are worth making. Its sample reads the
docs source and dates each item by lastModified; for a blog, read your
posts and use each post's own date, so an old post doesn't jump to the top
of someone's reader when you fix a typo.
Our way: a few lines of XML
This site writes the feed by hand instead (trimmed a little here). RSS 2.0 is a small format (the spec), and writing it directly means one less dependency. The route lives beside the blog:
import { getPublishedPosts, SITE } from "@/lib/blog"
export const dynamic = "force-static"
const escape = (text: string) =>
text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
export function GET() {
const posts = getPublishedPosts()
if (posts.length === 0) return new Response("Not found", { status: 404 })
const items = posts
.map(
(post) => `<item>
<title>${escape(post.data.title)}</title>
<link>${SITE}${post.url}</link>
<guid isPermaLink="true">${SITE}${post.url}</guid>
<pubDate>${post.data.date.toUTCString()}</pubDate>
<description>${escape(post.data.description)}</description>
</item>`
)
.join("\n")
return new Response(
`<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Your blog</title>
<link>${SITE}/blog</link>
<description>What the blog is about.</description>
<atom:link href="${SITE}/blog/rss.xml" rel="self" type="application/rss+xml" />
${items}
</channel>
</rss>`,
{ headers: { "Content-Type": "application/rss+xml; charset=utf-8" } }
)
}A few details in there matter:
- Only published posts. The feed reads the same
getPublishedPosts()as the sitemap, so a draft or a members-only post can never appear in it (members-only posts). - Escape the text. A title with an
&in it breaks the whole feed otherwise. Thefeedpackage does this for you. - Built once.
force-static(or the guide'srevalidate = false) makes the feed a file built with the site, not work done on every request. - A stable
guid. Readers use it to tell posts apart, so it should never change. The post's address works.
Let readers find it
Readers look for the feed in the page's head. In Next.js that is
alternates.types in a page's metadata
(generateMetadata):
export const metadata: Metadata = {
alternates: {
types: { "application/rss+xml": "/blog/rss.xml" },
},
}Fumadocs' guide puts it in the root layout, so every page points to the feed. This site sets it on the blog's list and on each post. Either works; what matters is that pasting any blog address into a reader finds the feed.
Check it
Run the finished feed through the W3C Feed Validation Service, then add it to a real reader and publish a test post. If the post shows up with the right title, date and link, you're done.