Astro Sitemap and RSS — SEO Feeds Generation
In this tutorial, you will learn about Astro Sitemap and RSS. We cover key concepts, practical examples, and best practices to help you master this topic.
Learn Astro sitemap and RSS generation: configure sitemap.xml, create RSS feeds for blogs, and optimize for search engine discovery of your content.
In this lesson, you'll install the sitemap integration, generate RSS feeds from content collections, and configure both for optimal SEO.
What You'll Learn
How to generate sitemap.xml with the sitemap integration, create RSS feeds with @astrojs/rss, filter pages, and customize feed metadata.
Why It Matters
Sitemaps help search engines discover all your pages. RSS feeds let users subscribe to your content. Both are essential for content-driven sites.
Real-World Use
DodaTech's tutorial site generates a sitemap with 15,000+ URLs and an RSS feed for new tutorial notifications.
flowchart LR
A[Site Pages] --> B[Sitemap Integration]
A --> C[RSS Generation]
B --> D[sitemap.xml]
C --> E[feed.xml]
D --> F[Search Engine Discovery]
style B fill:#ff5a03,color:#fff
Sitemap Integration
Install and configure:
npx astro add sitemap
import { defineConfig } from "astro/config";
import sitemap from "@astrojs/sitemap";
export default defineConfig({
site: "https://example.com",
integrations: [
sitemap({
filter: (page) => !page.includes("/admin/"),
changefreq: "weekly",
priority: 0.7,
lastmod: new Date(),
}),
],
});
Output: A sitemap.xml is generated at build time with all public page URLs listed with their change frequency and priority.
RSS Feed
Install @astrojs/rss:
npm install @astrojs/rss
Create an RSS endpoint:
---
// src/pages/feed.xml.ts
import rss from "@astrojs/rss";
import { getCollection } from "astro:content";
export async function GET() {
const posts = await getCollection("blog");
return rss({
title: "DodaTech Blog",
description: "Latest tutorials and articles",
site: "https://dodatech.com",
items: posts.map(post => ({
title: post.data.title,
pubDate: post.data.date,
description: post.data.description,
link: `/blog/${post.slug}/`,
})),
});
}
Output: A /feed.xml RSS feed listing all blog posts with titles, dates, descriptions, and links.
Customizing RSS
Add custom data and styling:
import rss from "@astrojs/rss";
import { getCollection } from "astro:content";
export async function GET(context) {
const posts = await getCollection("blog");
return rss({
title: "DodaTech Blog",
description: "Latest tutorials and articles",
site: context.site,
items: posts.map(post => ({
title: post.data.title,
pubDate: post.data.date,
description: post.data.description,
link: `/blog/${post.slug}/`,
categories: post.data.tags,
author: "DodaTech",
})),
customData: "<language>en-us</language>",
stylesheet: "/rss-style.xsl",
});
}
Filtering Sitemap Pages
Exclude specific routes:
sitemap({
filter: (page) => {
const exclude = ["/admin", "/draft", "/404"];
return !exclude.some(path => page.includes(path));
},
});
Common Mistakes
- Forgetting
sitein Astro config: Sitemap and RSS need thesiteURL. Without it, generated URLs are incorrect. - Not filtering out draft pages: Include drafts in your sitemap if you don't want them indexed. Filter
draft: truepages. - Missing
pubDatein RSS items: RSS feeds require a publication date for each item. Without it, feed readers may reject the feed. - Using
.xmlfiles insrc/pages/: Sitemap and RSS endpoints should be.tsfiles that return XML, not static.xmlfiles. - Not validating the sitemap: After generation, validate your
sitemap.xmlusing Google Search Console or an online validator.
Practice Questions
What does the sitemap integration generate? Answer: A
sitemap.xmlfile listing all public page URLs with metadata for search engines.How do you create an RSS feed in Astro? Answer: Create a
.tsfile insrc/pages/that imports@astrojs/rssand returns the RSS XML.Why filter pages in the sitemap config? Answer: To exclude admin pages, draft content, or other pages that shouldn't appear in search results.
What is the purpose of
changefreqandpriorityin sitemaps? Answer: They hint to search engines how often content changes and which pages are most important for crawling.
Challenge
Build a blog with both a sitemap and RSS feed. The sitemap should exclude draft posts and admin pages. The RSS feed should include categories and author information.
Mini Project
Create a content site with three content collections (blog, docs, tutorials). Generate a combined sitemap that includes all three collections and separate RSS feeds for each collection.
FAQ
What's Next
Learn about Astro View Transitions for smooth page animations and navigation transitions.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro