How To Generate RSS Feed For Your Sapper Website

How To Generate RSS Feed For Your Sapper Website
5 min read Sun Jul 18 2021

Adding an RSS feed to your website is a great way to get your readers notified when you create new blog posts.

What is an RSS Feed?

It is simply a file which is used to publish frequently updated works. Mostly, it’s used for blogs as it allows readers to be notified when new blog posts are created

Creating an RSS For Your Sapper Based Website

After learning the importance of RSS Feeds, I decided to add one to the blog section of my website. All the user has to do is to subscribe and they will be notified when new blog posts are made.

Creating the File

Inside the root of my blog folder /src/routes/blog, I created a file named feed.js. This is going to be the file which will return the RSS contents in XML format.

Writing the code

First of all, you will need to install the node-fetch module, you can simply install it by entering npm i node-fetch in your terminal.
After that, we open our feed.js file and write the following code.
I will explain below

import fetch from "node-fetch";

const get_items = async () => {
	let res = await fetch("");
	let posts = await res.json();
	return posts;

const sort_items = async () => {
	let posts = await get_items();
	let rss = ``;
	posts.forEach((post) => {
		rss += `\n
	return rss;

export function get(req, res, next) {
	let rss = sort_items().then((items) => {
		let feed = `<?xml version="1.0" encoding="UTF-8" ?>
  <rss version="2.0">
      <title>Kudadam Blog</title>
      <description>Lucretius' personal blog on tech, programming and stuff</description>
          <title>Kudadam Blog logo</title>

		res.setHeader("Content-Type", "application/xml");

So that’s all the code for the RSS Feed file.

Explaining the code

First of all, I defined an asynchronous function called get_items. This function performs a fetch call to and this returns all the blog posts in a JSON format.

Then I defined another function called sort_items which fetches the posts using the get_items functions, it then iterates through the metadata of each post and creates RSS tags for each metadata and returns all the blog posts containing the RSS tags.

Then the final function is the get function, this function is called when a request is made to the feed.js file, it first calls the sort_items function which also internally calls the get_items function, then it returns all the blog posts with their RSS tags.
Then, the returned results are placed in the RSS header template and returned to the user with content type as ‘xml’.

Share this article