Skip to content

Commit 85c6a20

Browse files
committed
feat(初步实现动态RSS):
1 parent 93939ce commit 85c6a20

File tree

5 files changed

+63
-13
lines changed

5 files changed

+63
-13
lines changed

Diff for: lib/rss.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { Feed } from 'feed'
55
import fs from 'fs'
66
import ReactDOMServer from 'react-dom/server'
77

8+
export const rssCacheHead = 'public, max-age=3600, stale-while-revalidate=59'
9+
810
/**
911
* 生成RSS内容
1012
* @param {*} post
@@ -25,11 +27,7 @@ const createFeedContent = async post => {
2527
}
2628
}
2729

28-
/**
29-
* 生成RSS数据
30-
* @param {*} props
31-
*/
32-
export async function generateRss(props) {
30+
export async function generateRssData(props) {
3331
const { NOTION_CONFIG, siteInfo, latestPosts } = props
3432
const TITLE = siteInfo?.title
3533
const DESCRIPTION = siteInfo?.description
@@ -39,12 +37,7 @@ export async function generateRss(props) {
3937
const SUB_PATH = NOTION_CONFIG?.SUB_PATH || BLOG.SUB_PATH
4038
const CONTACT_EMAIL = NOTION_CONFIG?.CONTACT_EMAIL || BLOG.CONTACT_EMAIL
4139

42-
// 检查 feed 文件是否在10分钟内更新过
43-
if (isFeedRecentlyUpdated('./public/rss/feed.xml', 10)) {
44-
return
45-
}
46-
47-
console.log('[RSS订阅] 生成/rss/feed.xml')
40+
console.log('[RSS订阅] 生成rss')
4841
const year = new Date().getFullYear()
4942
const feed = new Feed({
5043
title: TITLE,
@@ -68,6 +61,19 @@ export async function generateRss(props) {
6861
date: new Date(post?.publishDay)
6962
})
7063
}
64+
return feed
65+
}
66+
67+
/**
68+
* 生成RSS数据
69+
* @param {*} props
70+
*/
71+
export async function generateRss(props) {
72+
// 检查 feed 文件是否在10分钟内更新过
73+
if (isFeedRecentlyUpdated('./public/rss/feed.xml', 10)) {
74+
return
75+
}
76+
const feed = await generateRssData(props)
7177

7278
try {
7379
fs.mkdirSync('./public/rss', { recursive: true })

Diff for: pages/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import BLOG from '@/blog.config'
22
import { siteConfig } from '@/lib/config'
33
import { getGlobalData, getPostBlocks } from '@/lib/db/getSiteData'
44
import { generateRobotsTxt } from '@/lib/robots.txt'
5-
import { generateRss } from '@/lib/rss'
65
import { generateSitemapXml } from '@/lib/sitemap.xml'
76
import { DynamicLayout } from '@/themes/theme'
87

@@ -57,7 +56,7 @@ export async function getStaticProps(req) {
5756
// 生成robotTxt
5857
generateRobotsTxt(props)
5958
// 生成Feed订阅
60-
generateRss(props)
59+
// generateRss(props)
6160
// 生成
6261
generateSitemapXml(props)
6362

Diff for: pages/rss/atom.xml.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { getGlobalData } from '@/lib/db/getSiteData'
2+
import { generateRssData, rssCacheHead } from '@/lib/rss'
3+
4+
export async function getServerSideProps(ctx) {
5+
const from = 'rss-atom-xml-props'
6+
const { locale } = ctx.req
7+
const globalData = await getGlobalData({ from, locale })
8+
const rssData = await generateRssData(globalData)
9+
ctx.res.setHeader('Cache-Control', rssCacheHead)
10+
ctx.res.setHeader('Content-Type', 'text/xml')
11+
ctx.res.write(rssData.atom1()) // 直接返回内容
12+
ctx.res.end()
13+
}
14+
15+
export default function rssAtomXml() {}

Diff for: pages/rss/feed.json.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { getGlobalData } from '@/lib/db/getSiteData'
2+
import { generateRssData, rssCacheHead } from '@/lib/rss'
3+
4+
export async function getServerSideProps(ctx) {
5+
const from = 'rss-feed-json-props'
6+
const { locale } = ctx.req
7+
const globalData = await getGlobalData({ from, locale })
8+
const rssData = await generateRssData(globalData)
9+
ctx.res.setHeader('Cache-Control', rssCacheHead)
10+
ctx.res.setHeader('Content-Type', 'application/json')
11+
ctx.res.write(rssData.json1()) // 直接返回内容
12+
ctx.res.end()
13+
}
14+
15+
export default function rssFeedJson() {}

Diff for: pages/rss/feed.xml.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { getGlobalData } from '@/lib/db/getSiteData'
2+
import { generateRssData, rssCacheHead } from '@/lib/rss'
3+
4+
export async function getServerSideProps(ctx) {
5+
const from = 'rss-feed-xml-props'
6+
const { locale } = ctx.req
7+
const globalData = await getGlobalData({ from, locale })
8+
const rssData = await generateRssData(globalData)
9+
ctx.res.setHeader('Cache-Control', rssCacheHead)
10+
ctx.res.setHeader('Content-Type', 'text/xml')
11+
ctx.res.write(rssData.rss2()) // 直接返回内容
12+
ctx.res.end()
13+
}
14+
15+
export default function rssFeedXML() {}

0 commit comments

Comments
 (0)