โWhatโs on your newsfeed is feeding your mind.โ โ Jay Shetty, Think Like a Monk: Train Your Mind for Peace and Purpose Every Day
Social media newsfeeds aim to maximize engagement, often prioritizing popular content over educational value. In contrast, a customized RSS feed allows you to selectively subscribe to sources that enrich your mind with substantive ideas. By carefully curating thought-provoking sources aligned to your interests, an RSS feed provides focused knowledge to stimulate intellectual curiosity. Tailoring your RSS feed with enlightening sources fuels personal growth by making the most of your limited time.
Not only this, but also if you have a personal website with an RSS feed, it can help you stand out from the crowd in many ways. Having an RSS feed allows more frequent and efficient content indexing by search engines, establishes freshness signals, and improves website crawling - all beneficial for SEO. Furthermore, by curating an RSS feed with engaging topical content on your personal site, you can build a loyal audience and earn quality backlinks by getting your content shared and syndicated. In short, maintaining an active RSS feed fuels better SEO and allows personal websites to flourish by reaching the right readers.
I assume that you have a basic knowledge of Astro, one of the fastest growing SSG (not only limited to this now ๐คฏ) and its Content Collection. So, without further introduction letโs jump into the main part โโ
โ โ โ
Valid Parameters in RSS Feed for a Blog Post Item
Required Parameters
โข title: title of the blog post
โข description: description of the post
โข pubDate: publication date of the blog post
โข link: link to the blog post
Optional Parameters
โข categories: Catergories related to the post in an array
โข author: email address of the author of the post
โข commentsUrl: URL of a web page that contains comments on the item
โข source: An object that defines the title and url of the original feed for items that have been republished from another source.
โข enclosure: An object to specify properties for an included media source with three required values: url, length, and type
See ๐: @astrojs/rss Documentation
How to Ship Any Data to RSS Feed: The Tutorial
Important Points to Keep in Mind
โข The customData variable is used only for passing valid XML attributes like <language>en</language>, <atom:link /> etc. and it can not be passed inside an item tag. In this context, one thing to keep in mind about using customData variable that customData variable should be explicitly used here to pass custom valid XML attributes. No other variable xyz or myData etc. can be used here.
โข Each blog data is parsed inside a content tag in the final XML code. So, our custom data should also be inside the postโs corresponding content tag.
โข Each item has to contain only one content tag. If multiple content tags are passed, only one will be available in the final XML data.
Deep Diving into the code
Frontmatter Structure & Goal
I have several data associated with each of my blog post. The following is my frontmatter of this article โ
Out of all the items in the frontmatter, only title, description, cover image (enclosure), tags and publication date (pubDate) can be send to RSS feed by default. But, I want to send subtitle and duration (readeing time) of the post along with title, description, tags and pubDate, and show them on my styled webfeed page.
JavaScript Code to Send Data in RSS Feed
I have used ES6 template literals to merge all the custom data which should be shipped in the feed. However, before sending the data all of them should be converted to proper HTML syntax using marked and sanitize-html NPM packages. I have also attached a prefix to each of them (โSubtitle: โ and โDuration: โ) The complete javascript code is given below, and you can see, for each item, custom data like subtitle and duration are being passed through contentโ
As of now, the stylesheet (commented out) is not linked to the RSS feed, so that the parsed data can be viewed easily. Following is the screenshot of the content data [custom data is marked with a red rectangle] of this article encoded inside the feed.
Screenshot of the content section with marked area containing custom data in an unstyled RSS feed
Retrieve Custom Data to Show in Styled Web Feed
Now, we can see our each of the custom data is inside the <content:encoded></content:encoded> element under a <p></p> tag of an item. Here, we have to use some logic in our XSLT code to get our required data. The steps are like followingโ
โข Storing the whole text of each ๐ฐ๐ผ๐ป๐๐ฒ๐ป๐ element of an item inside a variable
โข Getting the required value using the ๐๐๐ฏ๐๐๐ฟ๐ถ๐ป๐ด-๐ฏ๐ฒ๐ณ๐ผ๐ฟ๐ฒ and ๐๐๐ฏ๐๐๐ฟ๐ถ๐ป๐ด-๐ฎ๐ณ๐๐ฒ๐ฟ functions
โ<p>Subtitle: โ and โ<p>Duration: โ are used to get the string after this parameters till a closing </p> tag. Following is the code for all items โ
Some headings, website description and styles are added later on which can be found in this GitHub gist.
Final View & Feed Validation
A screenshot of the styled web feed page is given below, and the red rectangular areas are indicating custom data (subtitle and duration) appeared on the page. However, the complete styled RSS feed page can be viewed on my website.
Screenshot of the styled web feed page with marked area containing the custom data
Now, itโs time for feed validation, and for that I used W3C Feed Validation Service. And yes, you gussed it right, my feed is validโ
โ โ โ
Thank you for reading.
I hope you found this โHow to Send Any Data to Your RSS Feed? A Guide with Astro + RSSโ article helpful. Please share if you like and leave any comment to let me know your thoughts. Also, subscribe to my Newsletter to get notification of my latest posts.
You can connect with me on LinkedIn, Instagram, Twitter or GitHub.