优化robots.txt\sitemap.xml

This commit is contained in:
tangly1024
2023-03-12 20:58:36 +08:00
parent c60828c80f
commit 9804d5b010
4 changed files with 84 additions and 61 deletions

View File

@@ -4,6 +4,8 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData'
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]
@@ -17,7 +19,6 @@ export async function getStaticProps() {
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}`,
description: siteInfo?.description,
@@ -47,6 +48,12 @@ export async function getStaticProps() {
if (JSON.parse(BLOG.ENABLE_RSS)) {
generateRss(props?.latestPosts || [])
}
// 生成robotTxt
generateRobotsTxt()
// 生成sitemap.xml
generateSitemapXml({ allPages: props.allPages })
delete props.allPages
return {
props: {

View File

@@ -1,60 +0,0 @@
// 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, s-maxage=10, stale-while-revalidate=59'
// )
return getServerSideSitemap(ctx, fields)
}
export default () => { }