How to generate an RSS feed file of your site

This recipe shows how to automatically generate an RSS feed for your blog.

Overview

In the same way you might iterate over a list of posts on your blog’s index page, this file will iterate over all the posts in your blog, generating an item for each one. This snipped assumes that your blog posts are in a _data.json file in a blog/ directory. If this is not the case for your site, feel free to modify it to suit your needs.

feed.xml.jade or feed.xml.ejs

All we need is a feed.xml.jade file if you’re using Jade, or feed.xml.ejs if you prefer EJS.

The naming of the file is important. The xxx.xml.xxx tells Harp that you want that file to be served as XML, which is the RSS format. Learn more about this on the Rules section under “Dead simple asset pipeline.”

RSS Feed script

The flexible metadata

Harp permits flexible metadata, so your data might be named differently. For this example, the blog metadata in in blog/_data.json and is structured like this:

If you’re using a Layout_layout.jade file, it will wrap this feed.xml.jade file, causing errors. Create a _data.json file in the same directory you’ve placed the feed.xml.jade file, to opt out of a layout:

{
"feed": {
"layout": false
}
}

Using EJS

Create a new file named feed.xml.ejs and copy/paste the snippet below:

If you’re using a Layout_layout.jade file, it will wrap this feed.xml.ejs file, causing errors. Create a _data.json file in the same directory you’ve placed the feed.xml.ejs file, to opt out of a Layout:

{
"feed": {
"layout": false
}
}

How does it work?

By iterating over the metadata in your _data.json file, you can create this RSS template just as you might create a list of your posts on a blog index page. By using variables that you store in your harp.json file, you can make sure that if anything changes, it will be easy to update everywhere at once.