Skip to content
Book a call

An RSS feed is still worth seventy-five lines of code

· 4-minute read · Measurement and growth · by Direct Softworks

A feed is the one subscription that needs no account, no email address and no consent banner. Ours is about seventy-five lines, and most of the thought went into five small decisions.

RSS is old enough that people assume it died. It did not; it went quiet. Feed readers are still maintained and still used, and a feed remains the one way to follow a site that needs no account, no email address and no consent from anyone.

A blog with no feed offers exactly one way to keep up with it: remember to come back. Ours had none until we rebuilt the blog index this autumn. It now has one at /blog/rss.xml, and the whole thing is about seventy-five lines of code, not counting comments, plus a test.

There is a small search bonus as well. Google accepts an RSS 2.0 feed as a sitemap format, noting that feeds usually describe recent URLs while an XML sitemap covers the whole site. Useful, and not the reason.

Build the XML in a pure function

The feed is a function that takes a list of posts and returns a string. The route handler only fetches the posts and sets the content type. Keeping the two apart means a plain Node script can test the XML without starting the framework or touching the database: it builds a feed from a few invented posts and checks the shape, the escaping, the dates and the links.

That test exists because XML has no forgiving mode. A browser repairs broken HTML and carries on. The XML specification requires a parser to stop normal processing at the first well-formedness error, so one bad character in one title can cost you the entire feed, not just the item it sits in.

So escaping is done in one place: the five XML entities, plus the control characters that XML 1.0 does not allow anywhere. One of those pasted into a title from a word processor would make the document malformed, and no escaping fixes a character the format forbids outright. They are dropped.

Five small decisions

The build date is the newest post, not now. The RSS 2.0 specification defines lastBuildDate as the last time the content of the channel changed. Stamping it with the request time would announce a change on every fetch, which is the same mistake as a sitemap lastmod that always says now, and we had already made that one once.

The guid is the canonical URL. The specification says an aggregator may use the guid to decide whether an item is new. Ours is the post's permalink without a trailing slash, because the slashed form answers with a redirect. If the guid ever changed form, a reader keying on it could treat every post as new and show the whole archive again.

Images go out as Media RSS, not enclosures. RSS's own enclosure element requires the file's length in bytes. A database row does not carry that, and inventing a number is worse than using the element made for images, so covers are sent as media:content, and only when they are paths on our own site. A feed that turned any absolute URL into an image would be vouching for somebody else's host.

Excerpts are cut on a word. Each item carries a plain-text excerpt of up to 300 characters, cut at the last space so a reader never shows half a word. Full text is the other common choice and suits some readers better. An excerpt sends them to the article page, where the links and related posts are.

An outage returns an empty feed, not an error. If the database cannot be reached, the route answers 200 with a valid channel and no items. A reader that gets an error backs off or flags the feed as broken; an empty channel simply has nothing new, and readers keep the items they already fetched. The feed is regenerated at most every five minutes, the same window as the blog index, so a new post reaches both together.

Making it findable

A feed nobody can find is just a file. The blog index declares it in the page head with a link of type application/rss+xml, which is how browsers and feed readers discover a feed from the page address alone, and the index sidebar carries a visible "Follow with RSS" link for everyone else.

Why bother

Because it is cheap and it is permanent. The feed has no subscriber list to manage, no consent to collect and no third party to depend on. It will keep working unattended for as long as the blog exists, and every reader who subscribes has chosen to hear from you in the most low-pressure way the web offers.

The general principle is that boring formats are the durable ones. A small, stable specification, five decisions written down next to the code, and a test that fails if the escaping breaks: that is the whole maintenance plan, and it will outlast most of what sits beside it on the page.

More notes