From d1ee0c156cee86e05bb9bd83884de7bb49585c06 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Tue, 21 Feb 2023 12:43:57 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4RSS=E6=96=B9=E6=A1=88?= =?UTF-8?q?=EF=BC=8C=E8=8A=82=E7=9C=81=E6=B5=81=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- lib/rss.js | 7 ++++++- next.config.js | 10 ++++++++++ pages/feed/index.js | 17 ----------------- pages/index.js | 6 ++++++ 5 files changed, 24 insertions(+), 19 deletions(-) delete mode 100644 pages/feed/index.js diff --git a/.gitignore b/.gitignore index a1a007c3..332741bc 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,5 @@ yarn-error.log* # sitemap /public/robots.txt -/public/sitemap.xml \ No newline at end of file +/public/sitemap.xml +/public/rss/* \ No newline at end of file diff --git a/lib/rss.js b/lib/rss.js index 9019f061..68214659 100644 --- a/lib/rss.js +++ b/lib/rss.js @@ -1,3 +1,4 @@ +import fs from 'fs' import { Feed } from 'feed' import BLOG from '@/blog.config' import ReactDOMServer from 'react-dom/server' @@ -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()) } diff --git a/next.config.js b/next.config.js index fb92a754..33b66908 100644 --- a/next.config.js +++ b/next.config.js @@ -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 [ { diff --git a/pages/feed/index.js b/pages/feed/index.js deleted file mode 100644 index 127f9402..00000000 --- a/pages/feed/index.js +++ /dev/null @@ -1,17 +0,0 @@ -import { generateRss } from '@/lib/rss' -import { getGlobalNotionData } from '@/lib/notion/getNotionData' - -export async function getServerSideProps ({ res }) { - res.setHeader('Content-Type', 'text/xml') - // 获取最新文章 - const globalNotionData = await getGlobalNotionData({ from: 'rss' }) - const xmlFeed = await generateRss(globalNotionData?.latestPosts || []) - res.write(xmlFeed) - res.end() - return { - props: {} - } -} - -const feed = () => null -export default feed diff --git a/pages/index.js b/pages/index.js index 952eadb6..a26d57bd 100644 --- a/pages/index.js +++ b/pages/index.js @@ -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] @@ -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}`, @@ -40,6 +43,9 @@ export async function getStaticProps() { } } + // 异步生成Feed订阅 + generateRss(props?.latestPosts || []) + return { props: { meta,