Skip to content
◀ Blog

Include full MDX posts in an Astro RSS feed

Build a full-content Astro RSS feed from MDX, render embedded components on the server, and check that the article body and figure captions reach feed readers.

Riley Hilliard4 min read

astromdxengineering

If your Astro RSS feed shows a title and a short description, open the XML and look for content:encoded inside an item. The RSS package writes that element when you give it the article HTML in the item’s content field. For an MDX post, render the post’s component to HTML first. Sending the raw body through a Markdown parser leaves the embedded components unrendered. Astro’s RSS recipe

Render MDX inside the RSS endpoint

A blog page can put the result of Astro’s render(post) straight into its template as <Content />. In an RSS endpoint, Astro’s experimental Container API turns that same component into an HTML string with container.renderToString(Content). Astro Container API reference

Here’s a minimal src/pages/rss.xml.ts for the collection format we use on Steamfield. It assumes a static Astro site with site set in the Astro config, the MDX integration and @astrojs/rss installed, and trusted posts with a publication status and a date. This version handles MDX with ordinary prose and server-rendered Astro components.

import rss from "@astrojs/rss";
import mdxRenderer from "@astrojs/mdx/server.js";
import { experimental_AstroContainer } from "astro/container";
import { getCollection, render } from "astro:content";

export async function GET() {
  const container = await experimental_AstroContainer.create();
  container.addServerRenderer({ name: "astro:jsx", renderer: mdxRenderer });
  const posts = await getCollection(
    "blog",
    (post) => post.data.status === "published",
  );
  posts.sort((a, b) => b.data.date.getTime() - a.data.date.getTime());

  const items = await Promise.all(
    posts.map(async (post) => {
      const { Content } = await render(post);
      return {
        title: post.data.title,
        description: post.data.description,
        link: `/blog/${post.id}`,
        pubDate: post.data.date,
        content: await container.renderToString(Content),
      };
    }),
  );

  return rss({
    site: import.meta.env.SITE,
    title: "Studio notes",
    description: "Articles from the studio",
    trailingSlash: false,
    items,
  });
}

The link assumes your blog route uses the collection entry’s ID as its slug. If your router strips extensions or treats a nested index.mdx specially, use the same path helper here. The item’s URL takes readers back to the page. Links and images inside its HTML have their own URLs. For posts that use relative paths, resolve those against the article’s canonical URL so an image such as ./figure.svg also loads in a feed reader.

Apply the same publication filter as the blog index. Reading every entry can send the full text of a draft to subscribers.

Give interactive figures a readable feed version

On Steamfield, some MDX files import Astro figures that wrap React demos. We register the React server renderer on the same container before rendering those posts.

import reactRenderer from "@astrojs/react/server.js";

// Inside GET, after creating the container
container.addServerRenderer({
  name: "@astrojs/react",
  renderer: reactRenderer,
});

A feed reader can display a figure caption, but the controls for an interactive demo belong on the page. We keep the caption outside the interactive component and strip the island and page scripts from the rendered feed HTML. In the parallel coding agents article, that leaves a caption explaining how four agents share three hosts. If the conclusion of your figure lives only inside a tooltip, write it into the caption before dropping the controls.

Check the built feed, including its last paragraph

For a static site, inspect dist/rss.xml after a production build. The XML may escape the angle brackets in the embedded HTML. An XML parser will decode the text for you. This example uses Python’s standard library to print the end of the newest item’s body.

from pathlib import Path
from xml.etree import ElementTree

feed = ElementTree.parse(Path("dist/rss.xml"))
item = feed.find("./channel/item")
if item is None:
    raise SystemExit("Feed contains no items")
body = item.findtext("{http://purl.org/rss/1.0/modules/content/}encoded")
if not body:
    raise SystemExit("Feed item has no content:encoded body")
print(item.findtext("link"))
print(body[-500:])

Compare that output with the end of the published article. For a post with a demo, also search the decoded body for a sentence from its caption. A valid XML document can pass a feed validator even when every item in it contains only the first paragraph.