RSS、Sitemap 与「可被发现」的博客
博客写完没人看?多半是没配 RSS 和 sitemap。喵~
RSS
// src/pages/rss.xml.ts
import rss from '@astrojs/rss';
import { getCollection } from 'astro:content';
export async function GET(context) {
const posts = (await getCollection('posts')).filter(p => !p.data.draft);
return rss({
title: '终端猫屋',
description: '一个终端风格的 Astro 博客主题',
site: context.site,
items: posts.map(p => ({
title: p.data.title,
pubDate: p.data.pubDate,
link: `/posts/${p.id}/`,
})),
});
}
Sitemap
@astrojs/sitemap 集成加进去就行,构建时自动生成 /sitemap-index.xml。
别忘了在
astro.config.mjs里设site,否则 RSS 和 sitemap 的链接都是相对的,订阅器和爬虫都不认。
小结
三行配置,换「可被发现」。这是博客的底线。喵~