Skip to content

Commit 1fdbb9f

Browse files
committed
Update resolveSitemap for generated sitemap
1 parent 61b285e commit 1fdbb9f

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ describe('resolveRouteData', () => {
110110
es: 'https://example.com/es',
111111
de: 'https://example.com/de',
112112
},
113+
media: {
114+
'(max-width: 600px)': 'https://example.com/mobile',
115+
'(min-width: 601px)': 'https://example.com/desktop',
116+
},
113117
},
114118
},
115119
])
@@ -120,6 +124,8 @@ describe('resolveRouteData', () => {
120124
<loc>https://example.com</loc>
121125
<xhtml:link rel="alternate" hreflang="es" href="https://example.com/es" />
122126
<xhtml:link rel="alternate" hreflang="de" href="https://example.com/de" />
127+
<xhtml:link rel="alternate" media="(max-width: 600px)" href="https://example.com/mobile" />
128+
<xhtml:link rel="alternate" media="(min-width: 601px)" href="https://example.com/desktop" />
123129
<lastmod>2021-01-01</lastmod>
124130
</url>
125131
</urlset>

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,22 @@ export function resolveSitemap(data: MetadataRoute.Sitemap): string {
7373
// Since sitemap is separated from the page rendering, there's not metadataBase accessible yet.
7474
// we give the default setting that won't effect the languages resolving.
7575
for (const language in languages) {
76+
const url = languages[language as keyof typeof languages]
77+
if (!url) continue
7678
content += `<xhtml:link rel="alternate" hreflang="${language}" href="${
77-
languages[language as keyof typeof languages]
79+
url
7880
}" />\n`
7981
}
8082
}
83+
84+
const medias = item.alternates?.media
85+
if (medias && Object.keys(medias).length) {
86+
for (const media in medias) {
87+
const url = medias[media as keyof typeof medias]
88+
if (!url) continue
89+
content += `<xhtml:link rel="alternate" media="${media}" href="${url}" />\n`
90+
}
91+
}
8192
if (item.images?.length) {
8293
for (const image of item.images) {
8394
content += `<image:image>\n<image:loc>${image}</image:loc>\n</image:image>\n`

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,9 @@ type SitemapFile = Array<{
712712
alternates?:
713713
| {
714714
languages?: Languages<string> | undefined
715+
media?: {
716+
[media: string]: string | undefined
717+
}
715718
}
716719
| undefined
717720
images?: string[] | undefined

0 commit comments

Comments
 (0)