Commit a85519e7 authored by Jiachi Liu's avatar Jiachi Liu

Fix sitemap generateSitemaps support for string id (#61088)

Fixes the string id that broken when sitemap is optimized to static
route.

When sitemap is optimized to static route in production, the route
argument is changed from `[[...__metadata_id__]]` to
`[__metadata_id__]`, so the type of it is also changed from array to
string that should reflect in the loader code.

Fixes #60894
Closes NEXT-2154
parent 0a2d7754
......@@ -122,8 +122,8 @@ const generateImageMetadata = imageModule.generateImageMetadata
${errorOnBadHandler(resourcePath)}
export async function GET(_, ctx) {
const { __metadata_id__ = [], ...params } = ctx.params || {}
const targetId = __metadata_id__[0]
const { __metadata_id__, ...params } = ctx.params || {}
const targetId = __metadata_id__?.[0]
let id = undefined
const imageMetadata = generateImageMetadata ? await generateImageMetadata({ params }) : null
......@@ -191,9 +191,16 @@ ${errorOnBadHandler(resourcePath)}
${'' /* re-export the userland route configs */}
export * from ${JSON.stringify(resourcePath)}
export async function GET(_, ctx) {
const { __metadata_id__ = [], ...params } = ctx.params || {}
const targetId = __metadata_id__[0]
const { __metadata_id__, ...params } = ctx.params || {}
${
'' /* sitemap will be optimized to [__metadata_id__] from [[..._metadata_id__]] in production */
}
const targetId = process.env.NODE_ENV !== 'production'
? __metadata_id__?.[0]
: __metadata_id__
let id = undefined
const sitemaps = generateSitemaps ? await generateSitemaps() : null
......
import { MetadataRoute } from 'next'
export async function generateSitemaps() {
return [{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }]
return [
{ id: 'child0' },
{ id: 'child1' },
{ id: 'child2' },
{ id: 'child3' },
]
}
export default function sitemap({ id }): MetadataRoute.Sitemap {
......
......@@ -197,7 +197,7 @@ createNextDescribe(
})
it('should support generate multi sitemaps with generateSitemaps', async () => {
const ids = [0, 1, 2]
const ids = ['child0', 'child1', 'child2', 'child3']
function fetchSitemap(id) {
return next
.fetch(
......@@ -558,7 +558,7 @@ createNextDescribe(
})
it('should generate static paths of dynamic sitemap in production', async () => {
const sitemapPaths = [0, 1, 2].map(
const sitemapPaths = ['child0', 'child1', 'child2', 'child3'].map(
(id) => `.next/server/app/gsp/sitemap/${id}.xml.meta`
)
const promises = sitemapPaths.map(async (filePath) => {
......
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