Unverified Commit 5fe33218 authored by Luud Janssen's avatar Luud Janssen Committed by GitHub

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: 's avatarJiachi Liu <4800338+huozhi@users.noreply.github.com>
parent 712669f6
......@@ -82,6 +82,8 @@ describe('resolveRouteData', () => {
{
url: 'https://example.com',
lastModified: '2021-01-01',
changeFrequency: 'weekly',
priority: 0.5,
},
])
).toMatchInlineSnapshot(`
......@@ -90,6 +92,8 @@ describe('resolveRouteData', () => {
<url>
<loc>https://example.com</loc>
<lastmod>2021-01-01</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
</urlset>
"
......
......@@ -47,19 +47,33 @@ export function resolveSitemap(data: MetadataRoute.Sitemap): string {
let content = ''
content += '<?xml version="1.0" encoding="UTF-8"?>\n'
content += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
for (const item of data) {
content += '<url>\n'
content += `<loc>${item.url}</loc>\n`
if (item.lastModified) {
content += `<lastmod>${
const serializedDate =
item.lastModified instanceof Date
? item.lastModified.toISOString()
: item.lastModified
}</lastmod>\n`
content += `<lastmod>${serializedDate}</lastmod>\n`
}
if (item.changeFrequency) {
content += `<changefreq>${item.changeFrequency}</changefreq>\n`
}
if (typeof item.priority === 'number') {
content += `<priority>${item.priority}</priority>\n`
}
content += '</url>\n'
}
content += '</urlset>\n'
return content
}
......
......@@ -558,6 +558,15 @@ type RobotsFile = {
type SitemapFile = Array<{
url: string
lastModified?: string | Date
changeFrequency?:
| 'always'
| 'hourly'
| 'daily'
| 'weekly'
| 'monthly'
| 'yearly'
| 'never'
priority?: number
}>
type ResolvingMetadata = Promise<ResolvedMetadata>
......
......@@ -6,6 +6,8 @@ export default function sitemap(): MetadataRoute.Sitemap {
{
url: 'https://example.com',
lastModified: '2021-01-01',
changeFrequency: 'weekly',
priority: 0.5,
},
{
url: 'https://example.com/about',
......
......@@ -55,6 +55,8 @@ createNextDescribe(
<url>
<loc>https://example.com</loc>
<lastmod>2021-01-01</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>https://example.com/about</loc>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment