Skip to content

Commit 31b7f79

Browse files
committed
Update resolveSitemap for generated sitemap
1 parent 7147642 commit 31b7f79

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ export function resolveSitemap(data: MetadataRoute.Sitemap): string {
7878
}" />\n`
7979
}
8080
}
81+
82+
const medias = item.alternates?.media
83+
if (medias && Object.keys(medias).length) {
84+
for (const media in medias) {
85+
content += `<xhtml:link rel="alternate" media="${media}" href="${
86+
medias[media as keyof typeof medias]
87+
}" />\n`
88+
}
89+
}
8190
if (item.images?.length) {
8291
for (const image of item.images) {
8392
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)