mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
Follow-up to #91189 which added support for accessing root params inside `generateStaticParams`. Previously, if a `generateStaticParams` function tried to read a root param that hasn't been defined yet — either because the current segment is supposed to define it, or because a child segment defines it — `getRootParam` would silently return `undefined`. This is especially problematic because #91019 will type root param getters as `Promise<string>` without `undefined`, making this a silent type safety violation. This change makes `getRootParam` throw an explicit error with the `generate-static-params` work unit store when the requested param is not present in `rootParams`.
45 lines
1.9 KiB
TypeScript
45 lines
1.9 KiB
TypeScript
import { nextTestSetup } from 'e2e-utils'
|
|
import path from 'path'
|
|
|
|
describe('app-root-param-getters - generateStaticParams error', () => {
|
|
const { next, isNextDev } = nextTestSetup({
|
|
files: path.join(__dirname, 'fixtures', 'generate-static-params-error'),
|
|
skipStart: true,
|
|
skipDeployment: true,
|
|
})
|
|
|
|
beforeAll(async () => {
|
|
try {
|
|
await next.start()
|
|
} catch {}
|
|
})
|
|
|
|
if (isNextDev) {
|
|
it('should error when reading a root param inside the generateStaticParams that defines it - dev', async () => {
|
|
const browser = await next.browser('/en/us')
|
|
|
|
// TODO: This is not the correct error code.
|
|
await expect(browser).toDisplayRedbox(`
|
|
{
|
|
"code": "E394",
|
|
"description": "Route /[lang]/[locale] used \`import('next/root-params').lang()\` inside \`generateStaticParams\`, but the \`lang\` parameter was not provided by a parent \`generateStaticParams\`. In \`generateStaticParams\`, root params are only available for segments nested below the segment that provides them.",
|
|
"environmentLabel": null,
|
|
"label": "Runtime Error",
|
|
"source": "app/[lang]/[locale]/layout.tsx (16:23) @ generateStaticParams
|
|
> 16 | const l = await lang()
|
|
| ^",
|
|
"stack": [
|
|
"generateStaticParams app/[lang]/[locale]/layout.tsx (16:23)",
|
|
],
|
|
}
|
|
`)
|
|
})
|
|
} else {
|
|
it('should error when reading a root param inside the generateStaticParams that defines it - build', async () => {
|
|
expect(next.cliOutput).toContain(
|
|
"Route /[lang]/[locale] used `import('next/root-params').lang()` inside `generateStaticParams`, but the `lang` parameter was not provided by a parent `generateStaticParams`. In `generateStaticParams`, root params are only available for segments nested below the segment that provides them."
|
|
)
|
|
})
|
|
}
|
|
})
|