Skip to content

Commit f9cac4e

Browse files
committed
feat: rss
1 parent 51adae7 commit f9cac4e

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

next.config.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ let nextConfig = {
2424

2525
return config;
2626
},
27+
async rewrites() {
28+
return [
29+
{
30+
source: '/rss',
31+
destination: '/feed.xml',
32+
},
33+
{
34+
source: '/rss.xml',
35+
destination: '/feed.xml',
36+
},
37+
{
38+
source: '/feed',
39+
destination: '/feed.xml',
40+
},
41+
]
42+
},
2743
productionBrowserSourceMaps: true,
2844
};
2945

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"prettier": "3.3.2",
4141
"react": "^18",
4242
"react-dom": "^18",
43+
"rss": "^1.2.2",
4344
"shiki": "^1.22.2",
4445
"tailwind-merge": "^2.5.4",
4546
"tailwind-scrollbar": "^3.1.0",
@@ -58,6 +59,7 @@
5859
"@types/node": "^20.16.11",
5960
"@types/react": "^18",
6061
"@types/react-dom": "^18",
62+
"@types/rss": "^0.0.32",
6163
"@types/typescript": "^2.0.0",
6264
"@typescript-eslint/eslint-plugin": "^8.8.1",
6365
"@typescript-eslint/parser": "^8.8.1",

pnpm-lock.yaml

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/feed.xml/route.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import RSS from 'rss';
2+
3+
import { siteUrl } from '~/seo';
4+
import { buildPostData } from '@/core';
5+
6+
const { postDataList } = buildPostData();
7+
8+
export async function GET() {
9+
const feed = new RSS({
10+
title: 'zhw blog',
11+
description: '记录我的生活',
12+
site_url: siteUrl.toString(),
13+
feed_url: `${siteUrl}/feed.xml`,
14+
language: 'zh-CN',
15+
image_url: `${siteUrl}/api/og`,
16+
generator: 'Next 14',
17+
});
18+
19+
postDataList.forEach((post) => {
20+
feed.item({
21+
title: post.title,
22+
guid: post.title,
23+
url: `${siteUrl}/notes/${post.title}`,
24+
description: post.summary ?? '',
25+
date: post.createdAt!,
26+
enclosure: {
27+
url: post.coverImage,
28+
},
29+
});
30+
});
31+
32+
return new Response(feed.xml(), {
33+
headers: {
34+
'content-type': 'application/xml',
35+
},
36+
});
37+
}

src/app/layout.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ import AccentColorStyleInjector from '@/components/modules/shared/AccentColorSty
88
import Root from '@/components/layout/Root';
99
import { seo } from '~/index';
1010
import { sansFont, serifFont } from '@/lib/fonts';
11+
import { siteUrl } from '~/seo';
1112

1213
export const metadata: Metadata = {
1314
metadataBase: seo.url,
15+
alternates: {
16+
canonical: `${siteUrl}`,
17+
types: {
18+
'application/rss+xml': [{ url: 'feed.xml', title: 'RSS 订阅' }],
19+
},
20+
},
1421
title: {
1522
template: seo.template,
1623
default: seo.title,

0 commit comments

Comments
 (0)