RSS
Astro supports fast, automatic RSS feed generation for blogs and other content websites. For more information about RSS feeds in general, see aboutfeeds.com.
Setting up @astrojs/rss
Section titled Setting up @astrojs/rssThe @astrojs/rss
package provides helpers for generating RSS feeds using API endpoints. This unlocks both static builds and on-demand generation when using an SSR adapter.
First, install @astrojs/rss
using your preferred package manager:
Then, ensure you’ve configured a site
in your project’s astro.config
. You will use this to generate links in your RSS feed via the SITE
environment variable.
Now, let’s generate our first RSS feed! Create an rss.xml.js
file under your src/pages/
directory. rss.xml
will be the output URL, so feel free to rename this if you prefer.
Next, import the rss
helper from the @astrojs/rss
package and call with the following parameters:
Generating items
Section titled Generating itemsThe items
field accepts either:
- An
import.meta.glob(...)
result (only use this for.md
files within thesrc/pages/
directory!) - A list of RSS feed objects, each with a
link
,title
,pubDate
, and optionaldescription
andcustomData
fields.
1. import.meta.glob
result
Section titled 1. import.meta.glob resultWe recommend this option as a convenient shorthand for .md
files under src/pages/
. Each post should have a title
, pubDate
, and optional description
and customData
fields in its frontmatter. If this isn’t possible, or you’d prefer to generate this frontmatter in code, see option 2.
Say your blog posts are stored under the src/pages/blog/
directory. You can generate an RSS feed like so:
See Vite’s glob import documentation for more on this import syntax.
2. List of RSS feed objects
Section titled 2. List of RSS feed objectsWe recommend this option for .md
files outside of the pages
directory. This is common when generating routes via getStaticPaths
.
For instance, say your .md
posts are stored under a src/posts/
directory. Each post has a title
, pubDate
, and slug
in its frontmatter, where slug
corresponds to the output URL on your site. We can generate an RSS feed using Vite’s import.meta.glob
helper like so:
Adding a stylesheet
Section titled Adding a stylesheetYou can style your RSS feed for a more pleasant user experience when viewing the file in your browser.
Use the rss
function’s stylesheet
option to specify an absolute path to your stylesheet.
If you don’t have an RSS stylesheet in mind, we recommend the Pretty Feed v3 default stylesheet, which you can download from GitHub and save into your project’s public/
directory.