Skip to content

Commit 5fe3321

Browse files
LuudJanssenhuozhi
andauthored
Add changeFrequency and priority attributes to sitemaps (#48484)
Closes #48479. Couldn't find the source for the Next.js beta docs, so I didn't add documentation. Co-authored-by: Jiachi Liu <[email protected]>
1 parent 712669f commit 5fe3321

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

packages/next/src/build/webpack/loaders/metadata/resolve-route-data.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ describe('resolveRouteData', () => {
8282
{
8383
url: 'https://example.com',
8484
lastModified: '2021-01-01',
85+
changeFrequency: 'weekly',
86+
priority: 0.5,
8587
},
8688
])
8789
).toMatchInlineSnapshot(`
@@ -90,6 +92,8 @@ describe('resolveRouteData', () => {
9092
<url>
9193
<loc>https://example.com</loc>
9294
<lastmod>2021-01-01</lastmod>
95+
<changefreq>weekly</changefreq>
96+
<priority>0.5</priority>
9397
</url>
9498
</urlset>
9599
"

packages/next/src/build/webpack/loaders/metadata/resolve-route-data.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,33 @@ export function resolveSitemap(data: MetadataRoute.Sitemap): string {
4747
let content = ''
4848
content += '<?xml version="1.0" encoding="UTF-8"?>\n'
4949
content += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
50+
5051
for (const item of data) {
5152
content += '<url>\n'
5253
content += `<loc>${item.url}</loc>\n`
54+
5355
if (item.lastModified) {
54-
content += `<lastmod>${
56+
const serializedDate =
5557
item.lastModified instanceof Date
5658
? item.lastModified.toISOString()
5759
: item.lastModified
58-
}</lastmod>\n`
60+
61+
content += `<lastmod>${serializedDate}</lastmod>\n`
62+
}
63+
64+
if (item.changeFrequency) {
65+
content += `<changefreq>${item.changeFrequency}</changefreq>\n`
5966
}
67+
68+
if (typeof item.priority === 'number') {
69+
content += `<priority>${item.priority}</priority>\n`
70+
}
71+
6072
content += '</url>\n'
6173
}
74+
6275
content += '</urlset>\n'
76+
6377
return content
6478
}
6579

packages/next/src/lib/metadata/types/metadata-interface.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,15 @@ type RobotsFile = {
558558
type SitemapFile = Array<{
559559
url: string
560560
lastModified?: string | Date
561+
changeFrequency?:
562+
| 'always'
563+
| 'hourly'
564+
| 'daily'
565+
| 'weekly'
566+
| 'monthly'
567+
| 'yearly'
568+
| 'never'
569+
priority?: number
561570
}>
562571

563572
type ResolvingMetadata = Promise<ResolvedMetadata>

test/e2e/app-dir/metadata-dynamic-routes/app/sitemap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export default function sitemap(): MetadataRoute.Sitemap {
66
{
77
url: 'https://example.com',
88
lastModified: '2021-01-01',
9+
changeFrequency: 'weekly',
10+
priority: 0.5,
911
},
1012
{
1113
url: 'https://example.com/about',

test/e2e/app-dir/metadata-dynamic-routes/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ createNextDescribe(
5555
<url>
5656
<loc>https://example.com</loc>
5757
<lastmod>2021-01-01</lastmod>
58+
<changefreq>weekly</changefreq>
59+
<priority>0.5</priority>
5860
</url>
5961
<url>
6062
<loc>https://example.com/about</loc>

0 commit comments

Comments
 (0)