Skip Navigation
Go to home page

How I Added an RSS Feed to My Next.js Site

I recently came across a tweet from Sara Soueidan applauding folks who provide an RSS feed on their site. Sara is someone I highly admire in frontend web development and accessibility, but I had so little knowledge of RSS feeds and so many questions:

  • What the heck are they?
  • What are they for?
  • I thought people didn't use them anymore?

If you also have questions like this, I recommend reading How Do RSS Feeds Work? right from The first few words really sum it up:

Imagine being able to log into one dashboard and getting the latest news and events from all of your favorite websites, blogs, or podcasts? With RSS feeds, it’s possible!

To better understand how RSS feeds are consumed, I created my own RSS reader app and I also wrote about it so you can create one too if you like. The app I created also served as a great way for me to test my RSS feed as I built it!

How I Added an RSS Feed to my Next.js Site

There were two main problems I ran into while trying to build an RSS feed for my articles. Firstly, I didn't know that neither the <description> nor content of each <item> in the channel can't have regular HTML in it. The HTML tags must be encoded. Secondly, I found out it's really difficult to try to parse the HTML content in a Node.js environment on my own before putting it into the feed.

After struggling for hours and trying several different approaches, I went to Twitter for help. There were a lot of helpful replies! I found exactly what I ended up using thanks to an example project from @vphreak. So, what did it take?

First, I installed the feed package.

1yarn add feed

Then, I added a generateRSSFeed function.

1const generateRSSFeed = (articles) => {
2  const baseUrl = '';
3  const author = {
4    name: 'Ashlee Boyer',
5    email: '',
6    link: '',
7  };
9  // Construct a new Feed object
10  const feed = new Feed({
11    title: 'Articles by Ashlee M Boyer',
12    description:
13      "You can find me talking about issues surrounding Disability, Accessibility, and Mental Health on Twitter, or you can find me regularly live-knitting or live-coding on Twitch. I'm @AshleeMBoyer on all the platforms I use.",
14    id: baseUrl,
15    link: baseUrl,
16    language: 'en',
17    feedLinks: {
18      rss2: `${baseUrl}/rss.xml`,
19    },
20    author,
21  });
23  // Add each article to the feed
24  articles.forEach((post) => {
25    const {
26      content,
27      fileName,
28      meta: { date, description, title },
29    } = post;
30    const url = `${baseUrl}/${fileName}`;
32    feed.addItem({
33      title,
34      id: url,
35      link: url,
36      description,
37      content,
38      author: [author],
39      date: new Date(date),
40    });
41  });
43  // Write the RSS output to a public file, making it
44  // accessible at
45  fs.writeFileSync('public/rss.xml', feed.rss2());

Finally, I updated the getStaticProps function exported from src/pages/index.jsx to call the new generateRSSFeed function.

1export async function getStaticProps() {
2  const articles = getAllArticles();
3  articles.sort((a, b) => ( < ? 1 : -1)
5  generateRSSFeed(articles);
7  return { props: { articles } };

Because my site is set up to build only when my main git branch is updated, getStaticProps will be called at that time and so will generateRSSFeed. This means the feed is always up to date when I push a new Markdown file for each post or whenever existing files are updated.


Back to Top