DEV Community


RSS/Atom and Site Map for Svelte/Sapper Blog - Part 3

Cleve Littlefield
Originally published at ・3 min read

I feel like a boomer when I tell people I still use an RSS reader. I still find it is the best way to keep up with interesting people writing about technology. I often share my blog roll with other programmers when they ask and point them to my reader of choice, RSS Owl. So yes, I expect my blog to support RSS even in this day and age.

When I started with a template Svelte/Sapper Blog project, I was slightly surprised that it does not have a way to generate RSS/Atom out of the box. It took a bit of searching to see how others were implementing RSS and found this on David Lacourt's blog, which references a solution from the Svelte man Rich Harris himself..

Pretty straight forward, with a regular server-side route, just returning XML over JSON. I have to admit coming other technologies that I assumed this needed to be more complicated with a model and XML translation middleware. I worked with "frameworks" so much that a straightforward solution eluded me for the moment. I also blame working predominantly with Kubernetes and infrastructure for the last few years.

Since I static gen my site with sapper export and that works by traversing all links, I have a link to my feeds from one my pages, I used the privacy policy page.

While RSS would do the trick on its own, let's also do ATOM to be thorough. The real secret here is ATOM expects the date to be in RFC3339 format pulled from here.

import { getPosts } from './_posts.js';
import { siteUrl } from '../../stores/_config.js';

function toRFC3339(date) {

  function pad(n) {
      return n < 10 ? "0" + n : n;

  function timezoneOffset(offset) {
      var sign;
      if (offset === 0) {
          return "Z";
      sign = (offset > 0) ? "-" : "+";
      offset = Math.abs(offset);
      return sign + pad(Math.floor(offset / 60)) + ":" + pad(offset % 60);

  return date.getFullYear() + "-" +
      pad(date.getMonth() + 1) + "-" +
      pad(date.getDate()) + "T" +
      pad(date.getHours()) + ":" +
      pad(date.getMinutes()) + ":" +
      pad(date.getSeconds()) + 

function renderXmlAtomFeed(posts) {
  return `<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="">

  <title>Clever Dev Codes</title>
  <link href="${siteUrl}"/>
  <link rel="self" href="${siteUrl}/blog/atom" />
  <updated>${toRFC3339(new Date())}</updated>
    <name>Cleve Littlefield</name>
  ${ => `
    <link href="${siteUrl}/blog/${post.slug}"/>
    <updated>${toRFC3339(new Date(post.publishedDate))}</updated>
    <content type="xhtml">
      <div xmlns="">
    <summary type="xhtml">
      <div xmlns="">


export async function get(req, res) {

  res.writeHead(200, {
    'Cache-Control': `max-age=0, s-max-age=${600}`, // 10 minutes
    'Content-Type': 'application/atom+xml'

  const posts = await getPosts();
  const feed = renderXmlAtomFeed(posts);


How about a site map? Similar concept:

import { getPosts } from "./blog/_posts.js";
import { siteUrl } from "../stores/_config.js";

function renderSitemapXml(slugs) {
  return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="" xmlns:news="" xmlns:xhtml="" xmlns:mobile="" xmlns:image="" xmlns:video="">
    slug => `

export async function get(req, res) {
  res.writeHead(200, {
    "Cache-Control": `public, max-age=0, must-revalidate`,
    "Content-Type": "application/xml"

  const posts = await getPosts();
  const slugs = => `blog/${post.slug}`);
  const feed = renderSitemapXml([...slugs, "", "/blog"]);

Discussion (0)