Beginner

RSS

A standards-compliant Atom/RSS feed for the blog, generated with @11ty/eleventy-plugin-rss.

1 min read

Live Implementation

The feed is live at /feed.xml. Current entries, pulled straight from the posts collection:

How It Works

@11ty/eleventy-plugin-rss gives you a few filters — dateToRfc822, absoluteUrl, htmlToAbsoluteUrls — that handle the annoying parts of feed generation, like RFC-822 dates and turning relative links into absolute ones. feed.njk uses those filters while looping over collections.posts (already sorted newest-first) to build a standard RSS 2.0 document, with permalink set to /feed.xml so it lands at a predictable URL.

Since it reads from the same posts collection the blog listing uses, a new post shows up in the feed the moment it shows up on the site. There’s nothing separate to write.

Folder Structure

src/feed.njk                    ← permalink: /feed.xml, the feed template itself
src/blog/*.md                    ← posts collection source
src/_11ty/collections/posts.js ← posts(): sorts by date, newest first

Important Files

src/feed.njk

---
permalink: /feed.xml
eleventyExcludeFromCollections: true
---
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>{{ site.title }}</title>
  <subtitle>{{ site.description }}</subtitle>
  <link href="{{ site.url }}/feed.xml" rel="self"/>
  <link href="{{ site.url }}/"/>
  <updated>{{ collections.posts | getNewestCollectionItemDate | dateToRfc3339 }}</updated>
  <id>{{ site.url }}/</id>
  <author>
    <name>{{ site.author.name }}</name>
  </author>

  {% for post in collections.posts %}
  <entry>
    <title>{{ post.data.title }}</title>
    <link href="{{ post.url | absoluteUrl(site.url) }}"/>
    <updated>{{ post.date | dateToRfc3339 }}</updated>
    <id>{{ post.url | absoluteUrl(site.url) }}</id>
    <content type="html">{{ post.templateContent | htmlToAbsoluteUrls(site.url) }}</content>
  </entry>
  {% endfor %}
</feed>

Notes

  • Right now only the blog scaffold feeds this. You could fold demos into the same feed too by merging collections.demos into the loop.
  • feed.njk is .njk, not .md, because XML output shouldn’t go through the Markdown renderer.