🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
SEO••By Komal Nakrani

Astro RSS: Build a Feed with @astrojs/rss

Astro RSS setup with @astrojs/rss: a rss.xml.js endpoint from your content collection, full-content items, categories, a styled feed and autodiscovery links.

#Astro#RSS#SEO#Content Collections#Blog

To add an Astro RSS feed, install @astrojs/rss, create src/pages/rss.xml.js that returns rss({ title, description, site: context.site, items }), and map your content collection to items. Astro builds it as a static rss.xml file, and an autodiscovery <link> in your head lets feed readers find it.

The whole setup is one package and one endpoint. This guide covers the basic feed, then the parts people usually need next: drafts, full post content, categories, a readable browser view and multiple feeds.

Key Takeaways

  • @astrojs/rss turns an array of items into valid RSS 2.0 XML from an endpoint file.
  • Set site in astro.config.mjs; the feed needs it to build absolute links, and context.site passes it in.
  • Filter drafts and sort by date in getCollection() before mapping to items.
  • Add content to each item for full-text feeds; sanitize the HTML first.
  • Put <link rel="alternate" type="application/rss+xml"> in every page’s head so readers and browsers detect the feed.

Why an RSS feed still matters

RSS is how feed readers (Feedly, Inoreader, NetNewsWire), newsletter tools that send posts automatically, and many aggregators consume a blog. A feed is also a clean, structured list of your latest URLs with dates, which search engines can use for discovery alongside your sitemap.

It costs almost nothing to add and it doesn’t change your pages. For any site with a blog, changelog or news section, there’s little reason to skip it.

Install @astrojs/rss

npm install @astrojs/rss

It’s a helper library, not an integration, so there’s nothing to add to the integrations array. You do need site:

// astro.config.mjs
import { defineConfig } from 'astro/config';

export default defineConfig({
  site: 'https://www.example.com',
});

Create the feed endpoint

A file in src/pages/ that ends in .xml.js (or .xml.ts) is an endpoint that outputs rss.xml:

// src/pages/rss.xml.js
import rss from '@astrojs/rss';
import { getCollection } from 'astro:content';

export async function GET(context) {
  const posts = (await getCollection('blog'))
    .sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf());

  return rss({
    title: 'Example Studio Blog',
    description: 'Notes on building fast websites with Astro.',
    site: context.site,
    items: posts.map((post) => ({
      title: post.data.title,
      pubDate: post.data.pubDate,
      description: post.data.description,
      link: `/blog/${post.id}/`,
    })),
    customData: '<language>en-us</language>',
  });
}

Run npm run build and open dist/rss.xml. Each item has a title, an absolute link built from site plus the relative link, a publication date in RFC 822 format, and the description.

link must match the real URL of the post, including the trailing slash if your site uses one. A mismatch sends readers through a redirect, or to a 404 if the paths differ. The trailingSlash option on rss() controls whether the helper adds or removes slashes; leaving it at the default keeps what you wrote.

Schema for feed-friendly frontmatter

@astrojs/rss exports rssSchema, a Zod schema of the fields it understands. You can use it directly or extend it:

// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';
import { rssSchema } from '@astrojs/rss';

const blog = defineCollection({
  loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/blog' }),
  schema: rssSchema.extend({
    title: z.string(),
    description: z.string(),
    pubDate: z.coerce.date(),
    draft: z.boolean().default(false),
    tags: z.array(z.string()).default([]),
  }),
});

export const collections = { blog };

Requiring title, description and pubDate means a post missing any of them fails the build, instead of showing up in readers as an untitled item dated 1970. The Content Collections guide explains schemas in depth.

Exclude drafts and future posts

Filter in getCollection() so drafts never reach the feed:

// src/pages/rss.xml.js (excerpt)
const now = new Date();
const posts = (await getCollection('blog', ({ data }) =>
  !data.draft && data.pubDate <= now
)).sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf());

The pubDate <= now check hides scheduled posts until a build runs after their date. On a static site that means you need a rebuild on or after the publish date, either manually or from a scheduled CI job.

Use the same filter for your blog index and post routes. A post that’s in the feed but returns a 404 is worse than either alone.

Limit the number of items

Feeds don’t need your whole archive. Most readers poll the feed and keep their own history, so 20 to 50 recent items is plenty and keeps the file small:

items: posts.slice(0, 30).map((post) => ({ /* ... */ })),

Full-content feeds

With only description, readers show a summary and a link. Add content to include the full post. For Markdown posts, render the raw body to HTML and sanitize it:

npm install markdown-it sanitize-html
// src/pages/rss.xml.js
import rss from '@astrojs/rss';
import { getCollection } from 'astro:content';
import MarkdownIt from 'markdown-it';
import sanitizeHtml from 'sanitize-html';

const parser = new MarkdownIt();

export async function GET(context) {
  const posts = await getCollection('blog', ({ data }) => !data.draft);

  return rss({
    title: 'Example Studio Blog',
    description: 'Notes on building fast websites with Astro.',
    site: context.site,
    items: posts.map((post) => ({
      title: post.data.title,
      pubDate: post.data.pubDate,
      description: post.data.description,
      link: `/blog/${post.id}/`,
      content: sanitizeHtml(parser.render(post.body ?? ''), {
        allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']),
      }),
    })),
  });
}

This renders plain Markdown. MDX components and custom remark plugins won’t run through markdown-it, so posts that rely on them render without those parts. For MDX-heavy blogs, the full-content RSS tip covers rendering with Astro’s own pipeline instead.

Relative image paths in content also break in readers, since there’s no page to resolve them against. Use absolute URLs for images in posts you want to syndicate in full.

Categories, authors and enclosures

Each item accepts more fields:

items: posts.map((post) => ({
  title: post.data.title,
  pubDate: post.data.pubDate,
  description: post.data.description,
  link: `/blog/${post.id}/`,
  categories: post.data.tags,
  author: '[email protected] (Example Studio)',
  customData: `<comments>${context.site}blog/${post.id}/#comments</comments>`,
})),
  • categories becomes one <category> per tag. Some readers let users filter by them.
  • author in RSS 2.0 is formally an email address, optionally followed by a name in parentheses.
  • enclosure ({ url, length, type }) attaches a media file, used for audio or video feeds.
  • customData inserts raw XML into the item, for elements the helper doesn’t model.

If you add elements from another namespace (such as dc:creator or media:content), declare the namespace with the xmlns option on rss().

Make the feed readable in a browser

Opening raw XML in a browser shows a wall of tags. An XSLT stylesheet turns it into a readable page that explains what a feed is:

return rss({
  // ...
  stylesheet: '/rss/styles.xsl',
});

Put styles.xsl in public/rss/. Several open-source templates exist (search for “pretty feed XSL”); copy one and adjust the colors to match your site.

Help readers find the feed

Add an autodiscovery link to your base layout’s head, on every page:

---
// src/layouts/BaseLayout.astro (head excerpt)
const feedURL = new URL('rss.xml', Astro.site);
---
<link
  rel="alternate"
  type="application/rss+xml"
  title="Example Studio Blog"
  href={feedURL}
/>

Feed readers and browser extensions use this to detect the feed when someone pastes your home page URL. Also link it visibly in the footer or on the blog index, for people who look for the RSS icon.

Multiple feeds

Each endpoint file is its own feed. A per-tag feed uses a dynamic route:

// src/pages/tags/[tag]/rss.xml.js
import rss from '@astrojs/rss';
import { getCollection } from 'astro:content';

export async function getStaticPaths() {
  const posts = await getCollection('blog', ({ data }) => !data.draft);
  const tags = [...new Set(posts.flatMap((p) => p.data.tags))];
  return tags.map((tag) => ({
    params: { tag },
    props: { posts: posts.filter((p) => p.data.tags.includes(tag)) },
  }));
}

export async function GET(context) {
  const { tag } = context.params;
  return rss({
    title: `Example Studio: ${tag}`,
    description: `Posts tagged ${tag}.`,
    site: context.site,
    items: context.props.posts.map((post) => ({
      title: post.data.title,
      pubDate: post.data.pubDate,
      link: `/blog/${post.id}/`,
    })),
  });
}

If you also want a JSON Feed for tools that prefer it, the JSON Feed tip adds one alongside rss.xml.

Validate the feed

After deploying, paste the feed URL into the W3C Feed Validation Service. It flags invalid dates, relative links and malformed XML. Then subscribe in a real reader and check that titles, dates and content look right.

If you use a Komal theme, the RSS docs page shows where the feed endpoint lives in the theme. Themes in the Astro blog and editorial category are a good fit for any content-first site with a feed. The official Astro RSS recipe documents every option, and the Astro SEO checklist shows where the feed fits among sitemaps and structured data.

FAQ

Where is the RSS feed in an Astro site?

Wherever you put the endpoint. src/pages/rss.xml.js produces /rss.xml, and src/pages/feed.xml.js produces /feed.xml. Astro doesn’t create a feed until you add one.

site isn’t set in astro.config.mjs, or you didn’t pass context.site to rss(). Set site to your production URL and rebuild.

How do I include full post content in Astro RSS?

Add a content field to each item with the post rendered as HTML. For Markdown, markdown-it plus sanitize-html works; MDX posts need Astro’s own rendering pipeline to include components.

Does an RSS feed help SEO?

Indirectly. It gives search engines and aggregators another structured list of new URLs with dates, and it brings repeat readers through feed apps and automated newsletters. It doesn’t replace a sitemap.

Can I have an RSS feed on a static Astro site?

Yes. The endpoint runs at build time and writes rss.xml to dist/ like any other page, so it works on every static host.