Skip to content

Commit

Permalink
Merge pull request #3165 from tangly1024/pr/pei92/3161-2
Browse files Browse the repository at this point in the history
解决sitemap有重复loc的问题 #3161
  • Loading branch information
tangly1024 authored Jan 19, 2025
2 parents ecdf3e1 + c54dee7 commit 7641c93
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions pages/sitemap.xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getServerSideSitemap } from 'next-sitemap'
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)
Expand All @@ -26,6 +27,8 @@ export const getServerSideProps = async ctx => {
fields = fields.concat(localeFields)
}

fields = getUniqueFields(fields);

// 缓存
ctx.res.setHeader(
'Cache-Control',
Expand All @@ -38,40 +41,41 @@ function generateLocalesSitemap(link, allPages, locale) {
if (locale && locale.length > 0 && locale.indexOf('/') !== 0) {
locale = '/' + locale
}
const dateNow = new Date().toISOString().split('T')[0]
const defaultFields = [
{
loc: `${link}${locale}`,
lastmod: new Date().toISOString().split('T')[0],
lastmod: dateNow,
changefreq: 'daily',
priority: '0.7'
},
{
loc: `${link}${locale}/archive`,
lastmod: new Date().toISOString().split('T')[0],
lastmod: dateNow,
changefreq: 'daily',
priority: '0.7'
},
{
loc: `${link}${locale}/category`,
lastmod: new Date().toISOString().split('T')[0],
lastmod: dateNow,
changefreq: 'daily',
priority: '0.7'
},
{
loc: `${link}${locale}/rss/feed.xml`,
lastmod: new Date().toISOString().split('T')[0],
lastmod: dateNow,
changefreq: 'daily',
priority: '0.7'
},
{
loc: `${link}${locale}/search`,
lastmod: new Date().toISOString().split('T')[0],
lastmod: dateNow,
changefreq: 'daily',
priority: '0.7'
},
{
loc: `${link}${locale}/tag`,
lastmod: new Date().toISOString().split('T')[0],
lastmod: dateNow,
changefreq: 'daily',
priority: '0.7'
}
Expand All @@ -94,4 +98,18 @@ function generateLocalesSitemap(link, allPages, locale) {
return defaultFields.concat(postFields)
}

function getUniqueFields(fields) {
const uniqueFieldsMap = new Map();

fields.forEach(field => {
const existingField = uniqueFieldsMap.get(field.loc);

if (!existingField || new Date(field.lastmod) > new Date(existingField.lastmod)) {
uniqueFieldsMap.set(field.loc, field);
}
});

return Array.from(uniqueFieldsMap.values());
}

export default () => {}

0 comments on commit 7641c93

Please sign in to comment.