Skip to content

Commit 7641c93

Browse files
authored
Merge pull request #3165 from tangly1024/pr/pei92/3161-2
解决sitemap有重复loc的问题 #3161
2 parents ecdf3e1 + c54dee7 commit 7641c93

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

pages/sitemap.xml.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getServerSideSitemap } from 'next-sitemap'
88
export const getServerSideProps = async ctx => {
99
let fields = []
1010
const siteIds = BLOG.NOTION_PAGE_ID.split(',')
11+
1112
for (let index = 0; index < siteIds.length; index++) {
1213
const siteId = siteIds[index]
1314
const id = extractLangId(siteId)
@@ -26,6 +27,8 @@ export const getServerSideProps = async ctx => {
2627
fields = fields.concat(localeFields)
2728
}
2829

30+
fields = getUniqueFields(fields);
31+
2932
// 缓存
3033
ctx.res.setHeader(
3134
'Cache-Control',
@@ -38,40 +41,41 @@ function generateLocalesSitemap(link, allPages, locale) {
3841
if (locale && locale.length > 0 && locale.indexOf('/') !== 0) {
3942
locale = '/' + locale
4043
}
44+
const dateNow = new Date().toISOString().split('T')[0]
4145
const defaultFields = [
4246
{
4347
loc: `${link}${locale}`,
44-
lastmod: new Date().toISOString().split('T')[0],
48+
lastmod: dateNow,
4549
changefreq: 'daily',
4650
priority: '0.7'
4751
},
4852
{
4953
loc: `${link}${locale}/archive`,
50-
lastmod: new Date().toISOString().split('T')[0],
54+
lastmod: dateNow,
5155
changefreq: 'daily',
5256
priority: '0.7'
5357
},
5458
{
5559
loc: `${link}${locale}/category`,
56-
lastmod: new Date().toISOString().split('T')[0],
60+
lastmod: dateNow,
5761
changefreq: 'daily',
5862
priority: '0.7'
5963
},
6064
{
6165
loc: `${link}${locale}/rss/feed.xml`,
62-
lastmod: new Date().toISOString().split('T')[0],
66+
lastmod: dateNow,
6367
changefreq: 'daily',
6468
priority: '0.7'
6569
},
6670
{
6771
loc: `${link}${locale}/search`,
68-
lastmod: new Date().toISOString().split('T')[0],
72+
lastmod: dateNow,
6973
changefreq: 'daily',
7074
priority: '0.7'
7175
},
7276
{
7377
loc: `${link}${locale}/tag`,
74-
lastmod: new Date().toISOString().split('T')[0],
78+
lastmod: dateNow,
7579
changefreq: 'daily',
7680
priority: '0.7'
7781
}
@@ -94,4 +98,18 @@ function generateLocalesSitemap(link, allPages, locale) {
9498
return defaultFields.concat(postFields)
9599
}
96100

101+
function getUniqueFields(fields) {
102+
const uniqueFieldsMap = new Map();
103+
104+
fields.forEach(field => {
105+
const existingField = uniqueFieldsMap.get(field.loc);
106+
107+
if (!existingField || new Date(field.lastmod) > new Date(existingField.lastmod)) {
108+
uniqueFieldsMap.set(field.loc, field);
109+
}
110+
});
111+
112+
return Array.from(uniqueFieldsMap.values());
113+
}
114+
97115
export default () => {}

0 commit comments

Comments
 (0)