From a932368bfb951c4c4e376ca3ed19f323dec64eb3 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Mon, 13 Mar 2023 12:47:47 +0800 Subject: [PATCH 1/4] fix/file-write --- lib/robots.txt.js | 31 +++++++++++--------- lib/rss.js | 54 +++++++++++++++++++--------------- lib/sitemap.xml.js | 73 ++++++++++++++++++++++++---------------------- 3 files changed, 87 insertions(+), 71 deletions(-) diff --git a/lib/robots.txt.js b/lib/robots.txt.js index 2592e1ff..ca3d4cc2 100644 --- a/lib/robots.txt.js +++ b/lib/robots.txt.js @@ -3,17 +3,22 @@ import fs from 'fs' import BLOG from '@/blog.config' export async function generateRobotsTxt() { - fs.mkdirSync('./public', { recursive: true }) - fs.writeFileSync('./public/robots.txt', ` - # * - User-agent: * - Allow: / - - # Host - Host: ${BLOG.LINK} - - # Sitemaps - Sitemap: ${BLOG.LINK}/sitemap.xml - - `) + const content = ` + # * + User-agent: * + Allow: / + + # Host + Host: ${BLOG.LINK} + + # Sitemaps + Sitemap: ${BLOG.LINK}/sitemap.xml + + ` + try { + fs.writeFileSync('robots.txt', content) + fs.writeFileSync('./public/robots.txt', content) + } catch (error) { + console.warn('无法写入文件', error) + } } diff --git a/lib/rss.js b/lib/rss.js index 66723f93..8536854a 100644 --- a/lib/rss.js +++ b/lib/rss.js @@ -5,6 +5,26 @@ import ReactDOMServer from 'react-dom/server' import { getPostBlocks } from './notion' import NotionPage from '@/components/NotionPage' +/** + * 生成RSS内容 + * @param {*} post + * @returns + */ +const createFeedContent = async post => { + // 加密的文章内容只返回摘要 + if (post.password && post.password !== '') { + return post.summary + } + const blockMap = await getPostBlocks(post.id, 'rss-content') + if (blockMap) { + post.blockMap = blockMap + const content = ReactDOMServer.renderToString() + const regexExp = + /
.*?<\/svg>
.*?<\/div><\/div>
.*?<\/div><\/div><\/div><\/div>/g + return content.replace(regexExp, '') + } +} + export async function generateRss(posts) { const year = new Date().getFullYear() const feed = new Feed({ @@ -30,28 +50,16 @@ export async function generateRss(posts) { }) } - 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()) -} - -/** - * 生成RSS内容 - * @param {*} post - * @returns - */ -const createFeedContent = async post => { - // 加密的文章内容只返回摘要 - if (post.password && post.password !== '') { - return post.summary - } - const blockMap = await getPostBlocks(post.id, 'rss-content') - if (blockMap) { - post.blockMap = blockMap - const content = ReactDOMServer.renderToString() - const regexExp = - /
.*?<\/svg>
.*?<\/div><\/div>
.*?<\/div><\/div><\/div><\/div>/g - return content.replace(regexExp, '') + try { + 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()) + fs.mkdirSync('./rss', { recursive: true }) + fs.writeFileSync('./rss/feed.xml', feed.rss2()) + fs.writeFileSync('./rss/atom.xml', feed.atom1()) + fs.writeFileSync('./rss/feed.json', feed.json1()) + } catch (error) { + console.warn('无法写入文件', error) } } diff --git a/lib/sitemap.xml.js b/lib/sitemap.xml.js index 0e057610..c182b38b 100644 --- a/lib/sitemap.xml.js +++ b/lib/sitemap.xml.js @@ -3,49 +3,52 @@ import fs from 'fs' import BLOG from '@/blog.config' export async function generateSitemapXml({ allPages }) { - fs.mkdirSync('./public', { recursive: true }) + const urls = [{ + loc: `${BLOG.LINK}`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily' + }, { + loc: `${BLOG.LINK}/archive`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily' + }, { + loc: `${BLOG.LINK}/category`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily' + }, { + loc: `${BLOG.LINK}/tag`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily' + }] - const urls = [{ - loc: `${BLOG.LINK}`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily' - }, { - loc: `${BLOG.LINK}/archive`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily' - }, { - loc: `${BLOG.LINK}/category`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily' - }, { - loc: `${BLOG.LINK}/tag`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily' - }] - - allPages?.forEach(post => { - urls.push({ - loc: `${BLOG.LINK}/${post.slug}`, - lastmod: new Date(post?.date?.start_date || post?.createdTime).toISOString().split('T')[0], - changefreq: 'daily' + allPages?.forEach(post => { + urls.push({ + loc: `${BLOG.LINK}/${post.slug}`, + lastmod: new Date(post?.date?.start_date || post?.createdTime).toISOString().split('T')[0], + changefreq: 'daily' + }) }) - }) - const xml = createSitemapXml(urls) - fs.writeFileSync('./public/sitemap.xml', xml) -} + const xml = createSitemapXml(urls) + try { + fs.writeFileSync('sitemap.xml', xml) + fs.writeFileSync('./public/sitemap.xml', xml) + } catch (error) { + console.warn('无法写入文件', error) + } -function createSitemapXml(urls) { - let urlsXml = '' - urls.forEach(u => { - urlsXml += ` + + function createSitemapXml(urls) { + let urlsXml = '' + urls.forEach(u => { + urlsXml += ` ${u.loc} ${u.lastmod} ${u.changefreq} ` - }) + }) - return ` + return ` ` -} + } From 0b5c82c41c648a4b606e5b6bafe6adad5ac690d7 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Mon, 13 Mar 2023 12:49:41 +0800 Subject: [PATCH 2/4] build --- lib/sitemap.xml.js | 89 +++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/lib/sitemap.xml.js b/lib/sitemap.xml.js index c182b38b..d51b5e16 100644 --- a/lib/sitemap.xml.js +++ b/lib/sitemap.xml.js @@ -3,53 +3,52 @@ import fs from 'fs' import BLOG from '@/blog.config' export async function generateSitemapXml({ allPages }) { - const urls = [{ - loc: `${BLOG.LINK}`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily' - }, { - loc: `${BLOG.LINK}/archive`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily' - }, { - loc: `${BLOG.LINK}/category`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily' - }, { - loc: `${BLOG.LINK}/tag`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily' - }] + const urls = [{ + loc: `${BLOG.LINK}`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily' + }, { + loc: `${BLOG.LINK}/archive`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily' + }, { + loc: `${BLOG.LINK}/category`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily' + }, { + loc: `${BLOG.LINK}/tag`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily' + }] - allPages?.forEach(post => { - urls.push({ - loc: `${BLOG.LINK}/${post.slug}`, - lastmod: new Date(post?.date?.start_date || post?.createdTime).toISOString().split('T')[0], - changefreq: 'daily' - }) + allPages?.forEach(post => { + urls.push({ + loc: `${BLOG.LINK}/${post.slug}`, + lastmod: new Date(post?.date?.start_date || post?.createdTime).toISOString().split('T')[0], + changefreq: 'daily' }) - const xml = createSitemapXml(urls) - try { - fs.writeFileSync('sitemap.xml', xml) - fs.writeFileSync('./public/sitemap.xml', xml) - } catch (error) { - console.warn('无法写入文件', error) - } + }) + const xml = createSitemapXml(urls) + try { + fs.writeFileSync('sitemap.xml', xml) + fs.writeFileSync('./public/sitemap.xml', xml) + } catch (error) { + console.warn('无法写入文件', error) + } +} +function createSitemapXml(urls) { + let urlsXml = '' + urls.forEach(u => { + urlsXml += ` +${u.loc} +${u.lastmod} +${u.changefreq} + +` + }) - - function createSitemapXml(urls) { - let urlsXml = '' - urls.forEach(u => { - urlsXml += ` - ${u.loc} - ${u.lastmod} - ${u.changefreq} - - ` - }) - - return ` - ` - } +} From 3221aea574063b96b0b153c850300681246c3515 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Mon, 13 Mar 2023 12:53:09 +0800 Subject: [PATCH 3/4] fix --- lib/sitemap.xml.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/sitemap.xml.js b/lib/sitemap.xml.js index d51b5e16..7522951d 100644 --- a/lib/sitemap.xml.js +++ b/lib/sitemap.xml.js @@ -40,11 +40,11 @@ function createSitemapXml(urls) { let urlsXml = '' urls.forEach(u => { urlsXml += ` -${u.loc} -${u.lastmod} -${u.changefreq} - -` + ${u.loc} + ${u.lastmod} + ${u.changefreq} + + ` }) return ` From b4446105cde8a07ad8e2ace3a9d5912e4af00115 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Mon, 13 Mar 2023 13:13:17 +0800 Subject: [PATCH 4/4] fix --- lib/robots.txt.js | 5 ++-- lib/rss.js | 8 +++--- pages/index.js | 3 --- pages/sitemap.xml.js | 60 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 pages/sitemap.xml.js diff --git a/lib/robots.txt.js b/lib/robots.txt.js index ca3d4cc2..4405164f 100644 --- a/lib/robots.txt.js +++ b/lib/robots.txt.js @@ -16,9 +16,10 @@ export async function generateRobotsTxt() { ` try { - fs.writeFileSync('robots.txt', content) + fs.mkdirSync('./public', { recursive: true }) fs.writeFileSync('./public/robots.txt', content) } catch (error) { - console.warn('无法写入文件', error) + // 在vercel运行环境是只读的,这里会报错; + // 但在vercel编译阶段、或VPS等其他平台这行代码会成功执行 } } diff --git a/lib/rss.js b/lib/rss.js index 8536854a..02121d49 100644 --- a/lib/rss.js +++ b/lib/rss.js @@ -55,11 +55,9 @@ export async function generateRss(posts) { fs.writeFileSync('./public/rss/feed.xml', feed.rss2()) fs.writeFileSync('./public/rss/atom.xml', feed.atom1()) fs.writeFileSync('./public/rss/feed.json', feed.json1()) - fs.mkdirSync('./rss', { recursive: true }) - fs.writeFileSync('./rss/feed.xml', feed.rss2()) - fs.writeFileSync('./rss/atom.xml', feed.atom1()) - fs.writeFileSync('./rss/feed.json', feed.json1()) } catch (error) { - console.warn('无法写入文件', error) + // 在vercel运行环境是只读的,这里会报错; + // 但在vercel编译阶段、或VPS等其他平台这行代码会成功执行 + // RSS被高频词访问将大量消耗服务端资源,故作为静态文件 } } diff --git a/pages/index.js b/pages/index.js index e8366de3..542500fb 100644 --- a/pages/index.js +++ b/pages/index.js @@ -5,7 +5,6 @@ import * as ThemeMap from '@/themes' import { useGlobal } from '@/lib/global' import { generateRss } from '@/lib/rss' import { generateRobotsTxt } from '@/lib/robots.txt' -import { generateSitemapXml } from '@/lib/sitemap.xml' const Index = props => { const { theme } = useGlobal() const ThemeComponents = ThemeMap[theme] @@ -46,8 +45,6 @@ export async function getStaticProps() { // 生成robotTxt generateRobotsTxt() - // 生成sitemap.xml - generateSitemapXml({ allPages: props.allPages }) // 生成Feed订阅 if (JSON.parse(BLOG.ENABLE_RSS)) { generateRss(props?.latestPosts || []) diff --git a/pages/sitemap.xml.js b/pages/sitemap.xml.js new file mode 100644 index 00000000..b17b3e45 --- /dev/null +++ b/pages/sitemap.xml.js @@ -0,0 +1,60 @@ +// pages/sitemap.xml.js +import { getServerSideSitemap } from 'next-sitemap' +import { getGlobalNotionData } from '@/lib/notion/getNotionData' +import BLOG from '@/blog.config' + +export const getServerSideProps = async (ctx) => { + const { allPages } = await getGlobalNotionData({ from: 'rss' }) + const defaultFields = [ + { + loc: `${BLOG.LINK}`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily', + priority: '0.7' + }, { + loc: `${BLOG.LINK}/archive`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily', + priority: '0.7' + }, { + loc: `${BLOG.LINK}/category`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily', + priority: '0.7' + }, { + loc: `${BLOG.LINK}/feed`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily', + priority: '0.7' + }, { + loc: `${BLOG.LINK}/search`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily', + priority: '0.7' + }, { + loc: `${BLOG.LINK}/tag`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily', + priority: '0.7' + } + ] + const postFields = allPages?.map(post => { + return { + loc: `${BLOG.LINK}/${post.slug}`, + lastmod: new Date(post?.date?.start_date || post?.createdTime).toISOString().split('T')[0], + changefreq: 'daily', + priority: '0.7' + } + }) + const fields = defaultFields.concat(postFields) + + // 缓存 + ctx.res.setHeader( + 'Cache-Control', + 'public, max-age=3600, stale-while-revalidate=59' + ) + + return getServerSideSitemap(ctx, fields) +} + +export default () => { }