AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Dynamic Sitemap Under Dynamic Segment Collapsed To `/-/Sitemap.Xml` After #93873

Next.js 16.3.x treats dynamic metadata routes with `generateStaticParams` as static metadata files, collapsing `app/[lang]/sitemap.ts` to `/-/sitemap.xml` and dropping per-locale sitemaps.

highConfidence 92%Next.jsAffected V16.3.0Affected V16.3.1Affected V16.3.2Affected V16.3.3Affected V16.3.4

Origin Analysis

In `isPageStatic` (`packages/next/src/build/utils.ts`), the shortcut for static metadata files only checks `isStaticMetadataFile(pathname)`. This matches dynamic metadata routes like `/[lang]/sitemap.xml` and bypasses `buildAppStaticPaths`, so the userland `generateStaticParams` exported by the sitemap route is never invoked. The metadata route loader re-exports `generateStaticParams` via `createReExportsCode`, but the shortcut skips it entirely, producing the placeholder `/-/sitemap.xml`.
1. Clone `https://github.com/Debbl/next-dynamic-sitemap-repro` 2. Checkout commit `d2e42032449c7f92113ba0e591b2979f4fd1de55` (plain repro, no patch) 3. Run `pnpm install` and `pnpm build` (uses `output: 'export'`) 4. Run `find out -name 'sitemap*'` 5. Observe only `out/-/sitemap.xml` exists; `out/en/sitemap.xml` and `out/zh/sitemap.xml` are missing

Fixing Code Block

Edge Case Audit

The fix relies on `segments` accurately containing the route's dynamic segments. If future refactors change how `segments` is populated for metadata routes, the guard may fail to trigger and the bug could reappear. If a static metadata file erroneously exports `generateStaticParams`, this fix will now execute that function and may produce additional outputs, which is intended but should be monitored. Rolling back to 16.2.3 restores the expected behavior but also reverts the #93873 optimization for all static metadata files. Use patch-package or wait for an official release; maintain the patch carefully when upgrading Next.js. Add integration tests for both `/[lang]/sitemap.ts` with `generateStaticParams` and `/[lang]/icon.png` without it to prevent future regressions.

Ecosystem Topology