mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-13 15:09:13 +00:00
fix
This commit is contained in:
@@ -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等其他平台这行代码会成功执行
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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被高频词访问将大量消耗服务端资源,故作为静态文件
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 || [])
|
||||
|
||||
60
pages/sitemap.xml.js
Normal file
60
pages/sitemap.xml.js
Normal file
@@ -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 () => { }
|
||||
Reference in New Issue
Block a user