I want an RSS feed and Sitemap that aren’t a pain to make.
RSS is not easy. XML is a finicky specification that needs to be encoded and formatted in a specific way to be valid. Often, its also difficult to gather up all of the content on a site into one concise rss endpoint.
Sapper ships with the ability to support json apis within the app by creating a index.json.js
file within any route
directory.
To create a route with the .xml
extension, create an rss.xml.js
file inside of the route directory. Inside of this route, we can use node-fetch
against our own json api, then map through and create straightforward XML.
This can even be made more dynamic using a file or directory with Sapper dynamic parameters, an example of which can be found at this repo from Rich Harris, creator of Svelte.
A similar approach can be taken to generate a sitemap, but using Node’s readDirSync
function instead of fetch
to look through the project file structure and construct an XML tree. Because of the post-name/index.md
organization pattern I had to make some modifications, but this post by Zechtyounes helped get 90% of the way there.
A NOTE: if there are not <a>
tags with their href
pointing to these pages, they won’t be properly crawled when sapper export
is run. The command in the package.json
file can be modified to run with the --entry
flag to include them.