添加 RSS 订阅源
准备好…
- 安装 Astro 的包,用于为你的网站创建一个 RSS 订阅源
- 创建一个可以通过 RSS 订阅源阅读的订阅源
安装 Astro 的 RSS 包
Astro 提供了一个可自定义包,用于快速为你的网站添加一个 RSS 订阅源。
这个官方包会生成一个非 HTML 文档,其中包含有关你的所有博客文章的信息,可以被 Feedly、The Old Reader 等 订阅源阅读器(Feed Reader) 读取。每次重新构建你的网站时,该文档都会更新。
个人用户可以通过在订阅源阅读器中订阅你的订阅源,在你的网站上发布新博客文章时会收到通知,这使它成为一个受欢迎的博客功能。
在终端中,退出 Astro 开发服务器(Ctrl + C),然后运行以下命令以安装 Astro 的 RSS 包。
npm install @astrojs/rsspnpm add @astrojs/rssyarn add @astrojs/rss重新启动开发服务器,以继续预览你的 Astro 项目。
npm run devpnpm run devyarn run dev
创建一个 .xml 订阅源文件
在
src/pages/目录下创建一个名为rss.xml.js的新文件。将以下代码复制到这个新文档中。自定义
title和description属性,如果需要,可以在customData中指定不同的语言: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>`, }); }在 Astro 的配置文件中设置
site属性为你的网站自己的唯一 Netlify URL。import { defineConfig } from "astro/config"; export default defineConfig({ site: "https://example.com" });访问
http://localhost:4321/rss.xml,验证你是否可以在页面上看到(未格式化的)文本,每个.md文件都有一个item。每个item应包含博客文章的信息,例如title、url和description。:::tip[在阅读器中查看你的 RSS 订阅源]下载一个订阅源阅读器,或者注册一个在线订阅源阅读器服务,并通过添加你自己的 Netlify URL 订阅你的网站。你也可以将此链接分享给他人,让他们订阅你的文章,并在发布新文章时收到通知。:::