社交平台会改时间线,邮箱订阅会进垃圾箱。RSS 还在:读者用自己的阅读器,按你更新的时间收文章。个人站值得接,工作量比做一套推荐系统小得多。
最小可用的 feed
Next.js App Router 里加一个路由即可,例如 /feed.xml:
ts
export async function GET() {
const site = "https://codevfun.work";
const items = posts.map((post) => `
<item>
<title>${escapeXml(post.title)}</title>
<link>${site}/writing/${post.slug}</link>
<guid>${site}/writing/${post.slug}</guid>
<pubDate>${new Date(post.date).toUTCString()}</pubDate>
<description>${escapeXml(post.description)}</description>
</item>`);
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>日和</title>
<link>${site}</link>
<description>网页制作与日常记录</description>
${items.join("")}
</channel>
</rss>`;
return new Response(xml, {
headers: { "Content-Type": "application/rss+xml; charset=utf-8" },
});
}标题、摘要里的 < & 必须转义,不然阅读器直接解析失败。
让软件找得到
在 <head> 里放发现链接:
html
<link
rel="alternate"
type="application/rss+xml"
title="日和"
href="https://codevfun.work/feed.xml"
/>Next 的 metadata.alternates.types 会生成同样的标签。页脚再放一个「RSS」文字链接,给人点,也给爬虫一个入口。
更新时注意
guid用稳定的文章 URL,不要用每次生成的随机 idpubDate用发布日期;改错字不必改这个日期- 只放公开文章,加密和草稿不要进 feed
- 摘要就够了。整篇 HTML 塞进
description会让阅读器又慢又乱
本站的订阅地址是 /feed.xml。用 Feedly、NetNewsWire、Reeder 任意一个订阅即可。
