Creating an RSS feed with Next.js and Next-MDX

Amir R Muntasser April 12, 2021
Source

I recently decided to fully rebuild my blog from the ground up using Next.js. By default, there's no built-in support to auto-generate an RSS for blog posts like there are in so many starters for Gatsby. As such I needed to roll my own.

Ultimately, an RSS feed is just an XML file that gets updated with new entries as new posts are made. Lucklily, we can take advantage of the fact that committing a new blog post will trigger a new build of the site. Using that, we can update the build command in our to run a node script before our actual build like this:

Now we just need to update our (which lives in our root directory) to create a new XML file when it's run. To do that I'm going to use 3 packages: , , and .

First we need to create a new RSS feed.

Next we need to get the blog posts to load into this feed.

Then we need to iterate over our posts and add them to our feed.

Now we have our feed built out, we just need to generate the XML and write it out to a file.

This will write our file to the public folder of our Next.js app so that it will be at a publicly available URL at .

See the gist for the full code.

Discussion in the ATmosphere

Loading comments...