How to generate RSS.xml files from Sanity posts with gatsby-plugin-feed
Looking at your PR and the challenge you're facing, yes, it's definitely possible to combine allSanityPost and allMdx into a single RSS feed with gatsby-plugin-feed! The key is to merge the results from both GraphQL queries into one unified array before passing them to the feed serializer.
Here's the approach that should work:
{
resolve: `gatsby-plugin-feed`,
options: {
query: `
{
site {
siteMetadata {
title
description
siteUrl
site_url: siteUrl
}
}
}
`,
feeds: [
{
serialize: ({ query: { site, allSanityPost, allMdx } }) => {
// Combine both sources into a single array
const sanityPosts = allSanityPost.nodes.map(node => ({
title: node.title,
description: node.excerpt,
date: node.publishedAt,
url: site.siteMetadata.siteUrl + '/blog/' + node.slug.current,
guid: site.siteMetadata.siteUrl + '/blog/' + node.slug.current,
custom_elements: [{ "content:encoded": node._rawBody }],
}));
const mdxPosts = allMdx.nodes.map(node => ({
title: node.frontmatter.title,
description: node.excerpt,
date: node.frontmatter.date,
url: site.siteMetadata.siteUrl + node.fields.slug,
guid: site.siteMetadata.siteUrl + node.fields.slug,
custom_elements: [{ "content:encoded": node.html }],
}));
// Merge and sort by date (most recent first)
return [...sanityPosts, ...mdxPosts].sort((a, b) =>
new Date(b.date) - new Date(a.date)
);
},
query: `
{
allSanityPost(sort: {publishedAt: DESC}) {
nodes {
title
slug {
current
}
publishedAt
excerpt
_rawBody
}
}
allMdx(sort: {frontmatter: {date: DESC}}) {
nodes {
excerpt
html
fields {
slug
}
frontmatter {
title
date
}
}
}
}
`,
output: "/rss.xml",
title: "Your Site's RSS Feed",
},
],
},
}The critical parts:
- Query both sources in the same feed configuration
- Map each source to a common structure with the same fields (title, description, date, url, etc.)
- Combine the arrays using the spread operator
[...sanityPosts, ...mdxPosts] - Sort by date to ensure chronological order regardless of source
You'll need to adjust the field mappings based on your actual schema (like how you're storing content in Sanity, whether you're using portable text or markdown, etc.), but this pattern should give you a single rss.xml file with content from both sources merged together.
The reason you were getting two separate feeds before was likely because you had two separate feed objects in the feeds array. This approach keeps it as one feed configuration that pulls from multiple GraphQL sources.
Show original thread5 replies
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.