RSS
Guidelines for generating an RSS feed in Astro projects using @astrojs/rss.
astro
## Astro RSS Feed Generation Guidelines
1. Installation: Install the official RSS package:
```bash
npx astro add rss
# or pnpm add @astrojs/rss / yarn add @astrojs/rss
```
2. Prerequisite: Ensure your site's base URL is configured in `astro.config.mjs` under the `site` property. This is required for generating absolute URLs in the feed.
3. Create Endpoint: Create a new file in `src/pages/` to serve as your feed endpoint. Use the `.xml.js` or `.xml.ts` extension (e.g., `src/pages/rss.xml.js`). This file will generate the `rss.xml` feed.
4. Implement Endpoint:
- Import the `rss` helper from `@astrojs/rss`.
- Export an async `GET` function.
- Inside `GET`, call and return the `rss()` function, passing it a configuration object.
5. `rss()` Configuration Object:
- `title` (string, required): The title of your RSS feed (e.g., "My Blog").
- `description` (string, required): A short description of your feed.
- `site` (string, required): The root URL of your site. Access this via `context.site` inside the `GET` function.
- `items` (array, required): An array of objects, where each object represents an item (e.g., a blog post) in the feed. See step 6.
- `customData` (string, optional): Inject arbitrary XML tags into the feed's `<channel>` element (e.g., `<language>en-us</language>`).
- `stylesheet` (string, optional): Absolute path (from site root) to an XSL stylesheet to format the feed for browser viewing (e.g., `/rss/styles.xsl`).
- `trailingSlash` (boolean, optional): Defaults to `true`. Set to `false` if your site config uses `trailingSlash: "never"` to ensure link consistency.
6. Generating Feed `items`:
- From Content Collections:
- Use `getCollection('your-collection')`.
- Map the results, extracting necessary properties for each item object. Required properties are `link` (full URL to the post), `title`, and `pubDate` (JavaScript `Date` object). Optional: `description`, `content`.
- From Markdown/MDX in `src/pages/`:
- Use `import.meta.glob()` to get the files.
- Use the `pagesGlobToRssItems()` helper from `@astrojs/rss` to automatically convert the glob result into the required `items` array format. (Assumes frontmatter contains `title`, `pubDate`, `description`).
- Item Properties:
- `link`: Absolute URL to the content item.
- `title`: Title of the item.
- `pubDate`: Publication date (`Date` object).
- `description` (optional): Short summary.
- `content` (optional): Full HTML content of the item. Must be sanitized using a library like `sanitize-html` before inclusion. For Markdown, render the body to HTML first (e.g., using `markdown-it` or `post.compiledContent()` for glob imports).
- `customData` (optional): String for custom XML tags within the `<item>`.
7. Sanitizing HTML Content: If including full post content in the `content` property, always sanitize the generated HTML to prevent cross-site scripting (XSS) vulnerabilities. Use a library like `sanitize-html` and configure allowed tags (e.g., include `<img>` if needed).
8. Auto-Discovery (Recommended): Add a `<link>` tag to the `<head>` of your site's layout(s) to help browsers and feed readers automatically find the feed:
```html
<link rel="alternate" type="application/rss+xml" title="Your Feed Title"
href={new URL('/rss.xml', Astro.site)} />
```
(Adjust the `href` if your endpoint file has a different name).
Reference: [Astro RSS Feed Recipe](https://docs.astro.build/en/recipes/rss/)