RSS feeds let people get updates from your website automatically. Generating an RSS feed helps your visitors stay informed without visiting your site all the time.
RSS feed generation in Astro
import rss from '@astrojs/rss'; export const GET = () => { return rss({ title: 'Your Site Title', description: 'A short description of your site', site: import.meta.env.SITE, items: [ { title: 'Post Title', pubDate: new Date('2024-06-01'), description: 'Summary of the post', link: '/posts/post-title/', }, // more items... ], }); };
The rss function creates the RSS feed XML automatically.
The GET function exports the feed for Astro to serve at a URL.
import rss from '@astrojs/rss'; export const GET = () => { return rss({ title: 'My Blog', description: 'Latest posts from my blog', site: import.meta.env.SITE, items: [], }); };
import rss from '@astrojs/rss'; const posts = [ { title: 'Hello World', pubDate: new Date('2024-06-10'), description: 'My first post', link: '/posts/hello-world/', }, ]; export const GET = () => rss({ title: 'My Blog', description: 'Latest posts', site: import.meta.env.SITE, items: posts, });
This example creates an RSS feed for a blog with two posts. When users visit the feed URL, they get an XML file listing these posts with titles, dates, descriptions, and links.
import rss from '@astrojs/rss'; const posts = [ { title: 'Welcome to Astro', pubDate: new Date('2024-06-15'), description: 'Getting started with Astro framework.', link: '/posts/welcome-to-astro/', }, { title: 'Building a Blog', pubDate: new Date('2024-06-20'), description: 'How to build a blog with Astro.', link: '/posts/building-a-blog/', }, ]; export const GET = () => { return rss({ title: 'Astro Blog', description: 'Updates and tutorials about Astro.', site: import.meta.env.SITE, items: posts, }); };
Make sure import.meta.env.SITE is set to your website URL in your Astro config.
RSS feeds are XML files that many apps can read to show your updates.
Test your RSS feed URL in a browser or RSS reader to check it works.
RSS feed generation helps share your website updates automatically.
Astro provides a simple @astrojs/rss package to create feeds.
Define your posts as items with title, date, description, and link.
Practice
Solution
Step 1: Understand RSS feed purpose
RSS feeds are designed to share updates automatically with users or apps.Step 2: Match purpose with options
Only To automatically share website updates with subscribers describes sharing updates automatically, which is the core use of RSS.Final Answer:
To automatically share website updates with subscribers -> Option AQuick Check:
RSS feeds share updates = A [OK]
- Confusing RSS with website speed optimization
- Thinking RSS adds animations or login features
- Assuming RSS is for styling or layout
Solution
Step 1: Recall correct package and syntax
The official package is '@astrojs/rss' and the helper is named 'rss'.Step 2: Match correct import syntax
import { rss } from '@astrojs/rss'; uses correct named import from the correct package name.Final Answer:
import { rss } from '@astrojs/rss'; -> Option AQuick Check:
Correct package and named import = C [OK]
- Using default import instead of named import
- Wrong package name like 'astro/rss'
- Incorrect helper function name
link?
const posts = [
{ title: 'Hello', date: '2024-01-01', description: 'Intro post', slug: 'hello' }
];
export const get = () => rss({
title: 'My Blog',
site: 'https://example.com',
items: posts.map(post => ({
title: post.title,
pubDate: new Date(post.date),
description: post.description,
link: `/posts/${post.slug}`
}))
});Solution
Step 1: Analyze link construction in map
The link is created as `/posts/${post.slug}`, which results in '/posts/hello'.Step 2: Understand site prefix usage
The 'site' URL is not concatenated in the link property, so link remains relative.Final Answer:
/posts/hello -> Option DQuick Check:
Link uses relative path '/posts/hello' = B [OK]
- Assuming link includes full site URL
- Confusing slug placement in URL
- Ignoring template literal syntax
import { rss } from '@astrojs/rss';
const posts = [
{ title: 'Post 1', date: '2024-02-01', description: 'Desc', slug: 'post-1' }
];
export const get = () => rss({
title: 'Blog',
site: 'https://blog.com',
items: posts.map(post => ({
title: post.title,
pubDate: post.date,
description: post.description,
link: `/blog/${post.slug}`
}))
});Solution
Step 1: Check pubDate type requirement
The rss helper expects pubDate as a Date object, but here it's a string.Step 2: Verify other code parts
Import is correct, items array is valid, and link can be relative, so no other errors.Final Answer:
pubDate should be a Date object, not a string -> Option CQuick Check:
pubDate must be Date object = A [OK]
- Leaving pubDate as string causes feed errors
- Thinking link must be absolute URL
- Forgetting to import rss helper
const posts = [
{ title: 'Old Post', date: '2023-12-31', slug: 'old' },
{ title: 'New Post', date: '2024-01-02', slug: 'new' }
];
export const get = () => rss({
title: 'Filtered Blog',
site: 'https://filtered.com',
items: posts
.filter(post => ??? )
.map(post => ({
title: post.title,
pubDate: new Date(post.date),
link: `/posts/${post.slug}`
}))
});
What should replace ??? to filter correctly?Solution
Step 1: Understand date filtering logic
We want posts published after January 1, 2024, so dates on or after 2024-01-02.Step 2: Compare dates as Date objects
Comparing strings can be unreliable; converting to Date objects ensures correct comparison.Step 3: Choose correct comparison operator
Using '>=' with new Date('2024-01-02') includes posts from that date onward.Final Answer:
new Date(post.date) >= new Date('2024-01-02') -> Option BQuick Check:
Date objects with >= for filtering = D [OK]
- Comparing date strings directly leads to errors
- Using wrong comparison operator excludes needed posts
- Filtering with incorrect date boundary
Ready to prove it?
Pass the test and this pattern moves unknown
