Here is the skeleton of a RSS file format:
rss<rss version="2.0"> <channel> <title>Erik Martín Jordán</title> <link>https://erikmartinjordan.com</link> <description>Code, web development, tech and off-topic</description> <item> <title><![CDATA[ Quick way to transform a number into a string using JavaScript ]]></title> <link>https://erikmartinjordan.com/quick-way-number-string-javascript</link> <description><![CDATA[ Quick way to transform a number into a string using JavaScript. ]]></description> </item> <item> <title><![CDATA[ onBlur prevents onClick to execute ]]></title> <link>https://erikmartinjordan.com/onblur-prevents-onclick-react</link> <description><![CDATA[ Two solutions on how to prevent the onBlur event cancelling the onClick. ]]></description> </item> </channel> </rss>
In this example, I will configure the file to display the RSS from https://erikmartinjordan.com/rss.xml
.
The first step is to configure the Firebase server editing firebase.json
:
json"rewrites": [ { "source": "/rss.xml", "function": "buildRSS" }, { "source": "/sitemap.xml", "function": "buildSitemap" }, { "source": "!/sitemap.xml && !/rss.xml", "function": "preRender" } ]
If the user accesses /rss.xml
, Firebase will run the buildRSS
function. Whereas if the user accesses /sitemap.xml
, Firebase will run the buildSitemap
function.
Otherwise, it'll display a normal page using the preRender
function. If you don't know what a preRender
function does, you can check this post.
We'll create a Firebase function in ../functions/index.js
.
This function returns a string with the sitemap info:
javascriptexports.buildRSS = functions.https.onRequest((request, response) => { let rssHeader = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel>` let title = `<title>Erik Martín Jordán</title>` let link = `<link>https://erikmartinjordan.com</link>` let description = `<description>Code, web development, tech and off-topic</description>` // You can also generate the posts dinamically using a loop and Firebase's database let posts = `<item><title><![CDATA[ Quick way to transform a number into a string using JavaScript ]]></title><link>https://erikmartinjordan.com/quick-way-number-string-javascript</link><description><![CDATA[ Quick way to transform a number into a string using JavaScript. ]]></description></item><item><title><![CDATA[ onBlur prevents onClick to execute ]]></title><link>https://erikmartinjordan.com/onblur-prevents-onclick-react</link><description><![CDATA[ Two solutions on how to prevent the onBlur event cancelling the onClick. ]]></description></item>` let rssFooter = `</channel></rss>` let rssString = rssHeader + title + link + description + posts + rssFooter response.set('Content-Type', 'text/xml') response.status(200).send(rssString) })
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.