sitemap.xml手动实现

This commit is contained in:
tangly1024
2022-12-29 21:38:58 +08:00
parent cf254a2797
commit 8640eee392
2 changed files with 56 additions and 1 deletions

View File

@@ -14,7 +14,7 @@
},
"scripts": {
"dev": "next dev",
"build": "next build && next-sitemap --config next-sitemap.config.js",
"build": "next build",
"start": "next start",
"post-build": "next-sitemap --config next-sitemap.config.js",
"bundle-report": "ANALYZE=true yarn build"

55
pages/sitemap.xml.js Normal file
View File

@@ -0,0 +1,55 @@
// 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}`, // Absolute url
lastmod: new Date(),
changefreq: 'daily',
priority: '0.7'
}, {
loc: `${BLOG.LINK}/archive`, // Absolute url
lastmod: new Date(),
changefreq: 'daily',
priority: '0.7'
}, {
loc: `${BLOG.LINK}/category`, // Absolute url
lastmod: new Date(),
changefreq: 'daily',
priority: '0.7'
}, {
loc: `${BLOG.LINK}/feed`, // Absolute url
lastmod: new Date(),
changefreq: 'daily',
priority: '0.7'
}, {
loc: `${BLOG.LINK}/search`, // Absolute url
lastmod: new Date(),
changefreq: 'daily',
priority: '0.7'
}, {
loc: `${BLOG.LINK}/tag`, // Absolute url
lastmod: new Date(),
changefreq: 'daily',
priority: '0.7'
}
]
const postFields = allPages?.map(post => {
console.log(post)
return {
loc: `${BLOG.LINK}/${post.slug}`, // Absolute url
lastmod: new Date(post?.date?.start_date || post?.createdTime),
changefreq: 'daily',
priority: '0.7'
}
})
const fields = defaultFields.concat(postFields)
return getServerSideSitemap(ctx, fields)
}
export default () => { }