🔥 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)
Content & MDX••By Komal Nakrani

Astro RSS Full Content: Render MDX Into Your Feed

Build an Astro RSS full content feed: render Markdown and MDX posts with the Container API, make links absolute, sanitize the HTML and add it to @astrojs/rss items.

#Astro#RSS Full Content#RSS#MDX#Container API

For a full-content RSS feed in Astro, add a content field with each post’s HTML to the items you pass to @astrojs/rss. To get that HTML from Markdown and MDX with your components and plugins intact, render each entry’s <Content /> with Astro’s Container API, then make relative links absolute and sanitize the result.

Key Takeaways

  • description alone gives readers a summary; content puts the whole post in the feed.
  • The Container API renders <Content /> to a string, so MDX components and plugins work.
  • MDX needs the MDX container renderer, loaded with loadRenderers() from astro:container.
  • Rewrite src and href to absolute URLs, because feed readers have no page to resolve them against.

The basic idea

@astrojs/rss accepts a content string per item and writes it into <content:encoded>. The simplest way to fill it is rendering the raw Markdown with markdown-it, which our RSS feed guide shows. That skips MDX components and your remark plugins. Rendering through Astro itself avoids that.

Render entries with the Container API

npm install @astrojs/rss sanitize-html
// src/pages/rss.xml.js
import rss from '@astrojs/rss';
import { getCollection, render } from 'astro:content';
import { experimental_AstroContainer as AstroContainer } from 'astro/container';
import { loadRenderers } from 'astro:container';
import { getContainerRenderer } from '@astrojs/mdx/container-renderer';
import sanitizeHtml from 'sanitize-html';

export async function GET(context) {
  const renderers = await loadRenderers([getContainerRenderer()]);
  const container = await AstroContainer.create({ renderers });

  const posts = (await getCollection('blog', ({ data }) => !data.draft))
    .sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf())
    .slice(0, 20);

  const items = [];
  for (const post of posts) {
    const { Content } = await render(post);
    const html = await container.renderToString(Content);
    items.push({
      title: post.data.title,
      description: post.data.description,
      pubDate: post.data.pubDate,
      link: `/blog/${post.id}/`,
      content: sanitizeHtml(absolutize(html, context.site), {
        allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img', 'figure', 'figcaption']),
      }),
    });
  }

  return rss({
    title: 'Example Blog',
    description: 'Articles about building fast sites.',
    site: context.site,
    items,
  });
}

function absolutize(html, site) {
  return html.replace(/(href|src)="\/(?!\/)/g, `$1="${new URL('/', site).href}`);
}

What each piece does:

  • loadRenderers([getContainerRenderer()]) registers MDX so the container can render .mdx entries. Plain Markdown entries render without it.
  • render(post) gives you the same Content component your post page uses, so plugins and MDX components run.
  • absolutize() turns root-relative href="/..." and src="/_astro/..." into full URLs. Optimized images from astro:assets live under /_astro/, so this matters.
  • sanitize-html strips scripts, styles and anything a feed reader shouldn’t run. Interactive islands don’t work in readers anyway.

slice(0, 20) keeps the feed small. Full-content feeds grow quickly, and most readers only fetch recent items. The Container API guide covers other uses of renderToString().

Things that render differently in feeds

  • Islands and scripts. Components with client:* directives output their server HTML only. Make sure that HTML makes sense without JavaScript, or show a link to the post.
  • Scoped styles. Readers ignore your CSS. Components that rely on styles for layout (tabs, carousels) should degrade to readable markup.
  • srcset attributes. The regex above only rewrites src and href. If your images use srcset, either rewrite it too or drop it in sanitize-html by not allowing the attribute.

Test the output in a real reader, or paste it into the W3C feed validator, before announcing the feed.

Add an autodiscovery tag to your layout’s <head>:

<link rel="alternate" type="application/rss+xml" title="Example Blog" href={new URL('rss.xml', Astro.site)} />

Our RSS docs page covers the setup most blog themes use. Want a feed in JSON too? See the JSON Feed tip, which can reuse the same rendering loop.

FAQ

How do I include full post content in an Astro RSS feed?

Add a content property with the post’s HTML to each item passed to rss(). Render the HTML with the Container API (renderToString(Content)) so MDX and plugins work, make URLs absolute, and sanitize it.

Why are images broken in my Astro RSS feed?

The feed contains root-relative URLs like /_astro/image.webp, which readers can’t resolve. Rewrite src and href to absolute URLs using context.site, and make sure site is set in astro.config.mjs.

Is the Astro Container API stable?

It’s exported as experimental_AstroContainer, so its API may still change between minor releases. Pin your Astro version and check the release notes when upgrading if your feed depends on it.