Skip to content

Commit

Permalink
调整RSS方案,节省流量
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Feb 21, 2023
1 parent 957b9c4 commit d1ee0c1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ yarn-error.log*

# sitemap
/public/robots.txt
/public/sitemap.xml
/public/sitemap.xml
/public/rss/*
7 changes: 6 additions & 1 deletion lib/rss.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs'
import { Feed } from 'feed'
import BLOG from '@/blog.config'
import ReactDOMServer from 'react-dom/server'
Expand Down Expand Up @@ -44,5 +45,9 @@ export async function generateRss(posts) {
date: new Date(post?.date?.start_date || post?.createdTime)
})
}
return feed.atom1()

fs.mkdirSync('./public/rss', { recursive: true })
fs.writeFileSync('./public/rss/feed.xml', feed.rss2())
fs.writeFileSync('./public/rss/atom.xml', feed.atom1())
fs.writeFileSync('./public/rss/feed.json', feed.json1())
}
10 changes: 10 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ module.exports = withBundleAnalyzer({
'images.unsplash.com'
]
},
// 默认将feed重定向至 /public/rss/feed.xml
async redirects() {
return [
{
source: '/feed',
destination: '/rss/feed.xml',
permanent: true
}
]
},
async rewrites() {
return [
{
Expand Down
17 changes: 0 additions & 17 deletions pages/feed/index.js

This file was deleted.

6 changes: 6 additions & 0 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getPostBlocks } from '@/lib/notion'
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import * as ThemeMap from '@/themes'
import { useGlobal } from '@/lib/global'
import { generateRss } from '@/lib/rss'
const Index = props => {
const { theme } = useGlobal()
const ThemeComponents = ThemeMap[theme]
Expand All @@ -12,8 +13,10 @@ const Index = props => {
export async function getStaticProps() {
const from = 'index'
const props = await getGlobalNotionData({ from })

const { siteInfo } = props
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published')

delete props.allPages
const meta = {
title: `${siteInfo?.title} | ${siteInfo?.description}`,
Expand All @@ -40,6 +43,9 @@ export async function getStaticProps() {
}
}

// 异步生成Feed订阅
generateRss(props?.latestPosts || [])

return {
props: {
meta,
Expand Down

0 comments on commit d1ee0c1

Please sign in to comment.