多语言版本sitemap处理

This commit is contained in:
tangly1024.com
2024-04-10 18:18:09 +08:00
parent 8ce32b422e
commit 4b5212ab4c

View File

@@ -1,61 +1,94 @@
// pages/sitemap.xml.js // pages/sitemap.xml.js
import { getServerSideSitemap } from 'next-sitemap'
import { getGlobalData } from '@/lib/db/getSiteData'
import BLOG from '@/blog.config' import BLOG from '@/blog.config'
import { getNotionPageData } from '@/lib/db/getSiteData'
import { extractLangId, extractLangPrefix } from '@/lib/utils/pageId'
import { getServerSideSitemap } from 'next-sitemap'
export const getServerSideProps = async (ctx) => { export const getServerSideProps = async ctx => {
const { allPages } = await getGlobalData({ from: 'rss' }) let fields = []
const defaultFields = [ const siteIds = BLOG.NOTION_PAGE_ID.split(',')
{ for (let index = 0; index < siteIds.length; index++) {
loc: `${BLOG.LINK}`, const siteId = siteIds[index]
lastmod: new Date().toISOString().split('T')[0], const id = extractLangId(siteId)
changefreq: 'daily', const locale = extractLangPrefix(siteId)
priority: '0.7' // 第一个id站点默认语言
}, { const localeFields = generateLocalesSitemap(
loc: `${BLOG.LINK}/archive`, await getNotionPageData({
lastmod: new Date().toISOString().split('T')[0], pageId: id,
changefreq: 'daily', from: 'sitemap.xml'
priority: '0.7' }).allPages,
}, { locale
loc: `${BLOG.LINK}/category`, )
lastmod: new Date().toISOString().split('T')[0], fields = fields.concat(localeFields)
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?.filter(p => p.status === BLOG.NOTION_PROPERTY_NAME.status_publish)?.map(post => {
const slugWithoutLeadingSlash = post?.slug.startsWith('/') ? post?.slug?.slice(1) : post.slug
return {
loc: `${BLOG.LINK}/${slugWithoutLeadingSlash}`,
lastmod: new Date(post?.publishDay).toISOString().split('T')[0],
changefreq: 'daily',
priority: '0.7'
}
})
const fields = defaultFields.concat(postFields)
// 缓存 // 缓存
ctx.res.setHeader( ctx.res.setHeader(
'Cache-Control', 'Cache-Control',
'public, max-age=3600, stale-while-revalidate=59' 'public, max-age=3600, stale-while-revalidate=59'
) )
console.log('fff', fields)
return getServerSideSitemap(ctx, fields) return getServerSideSitemap(ctx, fields)
} }
export default () => { } function generateLocalesSitemap(allPages, locale) {
if (locale && locale.length > 0 && locale.indexOf('/') !== 0) {
locale = '/' + locale
}
const defaultFields = [
{
loc: `${BLOG.LINK}${locale}`,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily',
priority: '0.7'
},
{
loc: `${BLOG.LINK}${locale}/archive`,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily',
priority: '0.7'
},
{
loc: `${BLOG.LINK}${locale}/category`,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily',
priority: '0.7'
},
{
loc: `${BLOG.LINK}${locale}/feed`,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily',
priority: '0.7'
},
{
loc: `${BLOG.LINK}${locale}/search`,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily',
priority: '0.7'
},
{
loc: `${BLOG.LINK}${locale}/tag`,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily',
priority: '0.7'
}
]
const postFields =
allPages
?.filter(p => p.status === BLOG.NOTION_PROPERTY_NAME.status_publish)
?.map(post => {
const slugWithoutLeadingSlash = post?.slug.startsWith('/')
? post?.slug?.slice(1)
: post.slug
return {
loc: `${BLOG.LINK}${locale}/${slugWithoutLeadingSlash}`,
lastmod: new Date(post?.publishDay).toISOString().split('T')[0],
changefreq: 'daily',
priority: '0.7'
}
}) ?? []
return defaultFields.concat(postFields)
}
export default () => {}