From 4b5212ab4ccf3dc21e60b99ab365f8a582cefbc2 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Wed, 10 Apr 2024 18:18:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=ACsi?= =?UTF-8?q?temap=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/sitemap.xml.js | 131 +++++++++++++++++++++++++++---------------- 1 file changed, 82 insertions(+), 49 deletions(-) diff --git a/pages/sitemap.xml.js b/pages/sitemap.xml.js index a0967ede..1305818a 100644 --- a/pages/sitemap.xml.js +++ b/pages/sitemap.xml.js @@ -1,61 +1,94 @@ // pages/sitemap.xml.js -import { getServerSideSitemap } from 'next-sitemap' -import { getGlobalData } from '@/lib/db/getSiteData' 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) => { - const { allPages } = await getGlobalData({ 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?.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) +export const getServerSideProps = async ctx => { + let fields = [] + const siteIds = BLOG.NOTION_PAGE_ID.split(',') + for (let index = 0; index < siteIds.length; index++) { + const siteId = siteIds[index] + const id = extractLangId(siteId) + const locale = extractLangPrefix(siteId) + // 第一个id站点默认语言 + const localeFields = generateLocalesSitemap( + await getNotionPageData({ + pageId: id, + from: 'sitemap.xml' + }).allPages, + locale + ) + fields = fields.concat(localeFields) + } // 缓存 ctx.res.setHeader( 'Cache-Control', 'public, max-age=3600, stale-while-revalidate=59' ) - + console.log('fff', 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 () => {}