Add an RSS feed
本頁內容尚未翻譯。
準備好……
- Install an Astro package for creating an RSS feed for your website
- Create a feed that can be subscribed to and read by RSS feed readers
Install Astro's RSS package
Astro provides a custom package to quickly add an RSS feed to your website.
This official package generates a non-HTML document with information about all of your blog posts that can be read by feed readers like Feedly, The Old Reader, and more. This document is updated every time your site is rebuilt.
Individuals can subscribe to your feed in a feed reader, and receive a notification when you publish a new blog post on your site, making it a popular blog feature.
In your terminal, quit the Astro development server (Ctrl + C) and run the following command to install Astro's RSS package.
npm install @astrojs/rsspnpm add @astrojs/rssyarn add @astrojs/rssRestart the dev server to begin working on your Astro project again.
npm run devpnpm run devyarn run dev
Create an .xml feed document
Create a new file in
src/pages/calledrss.xml.jsCopy the following code into this new document. Customize the
titleanddescriptionproperties, and if necessary, specify a different language incustomData:import rss, { pagesGlobToRssItems } from '@astrojs/rss'; export async function GET(context) { return rss({ title: 'Astro Learner | Blog', description: 'My journey learning Astro', site: context.site, items: await pagesGlobToRssItems(import.meta.glob('./**/*.md')), customData: `<language>en-us</language>`, }); }Add the
siteproperty to the Astro config with your site's own unique Netlify URL.import { defineConfig } from "astro/config"; export default defineConfig({ site: "https://example.com" });Visit
http://localhost:4321/rss.xmland verify that you can see (unformatted) text on the page with anitemfor each of your.mdfiles. Each item should contain blog post information such astitle,url, anddescription.:::tip[View your RSS feed in a reader]Download a feed reader, or sign up for an online feed reader service and subscribe to your site by adding your own Netlify URL. You can also share this link with others so they can subscribe to your posts, and be notified when a new one is published.:::