Skip to content

Commit

Permalink
Fix sitemap generateSitemaps support for string id (#61088)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
huozhi committed Mar 4, 2024
1 parent 0a2d775 commit a85519e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion test/e2e/app-dir/metadata-dynamic-routes/app/gsp/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
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 {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/app-dir/metadata-dynamic-routes/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit a85519e

Please sign in to comment.