Skip to content

Commit d1ee0c1

Browse files
committed
调整RSS方案,节省流量
1 parent 957b9c4 commit d1ee0c1

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ yarn-error.log*
4343

4444
# sitemap
4545
/public/robots.txt
46-
/public/sitemap.xml
46+
/public/sitemap.xml
47+
/public/rss/*

lib/rss.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'fs'
12
import { Feed } from 'feed'
23
import BLOG from '@/blog.config'
34
import ReactDOMServer from 'react-dom/server'
@@ -44,5 +45,9 @@ export async function generateRss(posts) {
4445
date: new Date(post?.date?.start_date || post?.createdTime)
4546
})
4647
}
47-
return feed.atom1()
48+
49+
fs.mkdirSync('./public/rss', { recursive: true })
50+
fs.writeFileSync('./public/rss/feed.xml', feed.rss2())
51+
fs.writeFileSync('./public/rss/atom.xml', feed.atom1())
52+
fs.writeFileSync('./public/rss/feed.json', feed.json1())
4853
}

next.config.js

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ module.exports = withBundleAnalyzer({
1414
'images.unsplash.com'
1515
]
1616
},
17+
// 默认将feed重定向至 /public/rss/feed.xml
18+
async redirects() {
19+
return [
20+
{
21+
source: '/feed',
22+
destination: '/rss/feed.xml',
23+
permanent: true
24+
}
25+
]
26+
},
1727
async rewrites() {
1828
return [
1929
{

pages/feed/index.js

-17
This file was deleted.

pages/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getPostBlocks } from '@/lib/notion'
33
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
44
import * as ThemeMap from '@/themes'
55
import { useGlobal } from '@/lib/global'
6+
import { generateRss } from '@/lib/rss'
67
const Index = props => {
78
const { theme } = useGlobal()
89
const ThemeComponents = ThemeMap[theme]
@@ -12,8 +13,10 @@ const Index = props => {
1213
export async function getStaticProps() {
1314
const from = 'index'
1415
const props = await getGlobalNotionData({ from })
16+
1517
const { siteInfo } = props
1618
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published')
19+
1720
delete props.allPages
1821
const meta = {
1922
title: `${siteInfo?.title} | ${siteInfo?.description}`,
@@ -40,6 +43,9 @@ export async function getStaticProps() {
4043
}
4144
}
4245

46+
// 异步生成Feed订阅
47+
generateRss(props?.latestPosts || [])
48+
4349
return {
4450
props: {
4551
meta,

0 commit comments

Comments
 (0)