Unverified Commit a46ff7c8 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Error when accessing undefined root params in `generateStaticParams` (#91519)

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`.
parent 892f7df0
...@@ -1139,5 +1139,6 @@ ...@@ -1139,5 +1139,6 @@
"1138": "`unstable_retry()` can only be used in the App Router. Use `reset()` in the Pages Router.", "1138": "`unstable_retry()` can only be used in the App Router. Use `reset()` in the Pages Router.",
"1139": "`unstable_catchError` can only be used in Client Components.", "1139": "`unstable_catchError` can only be used in Client Components.",
"1140": "Route %s used \\`import('next/root-params').%s()\\` inside \\`\"use cache\"\\` nested within \\`unstable_cache\\`. Root params are not available in this context.", "1140": "Route %s used \\`import('next/root-params').%s()\\` inside \\`\"use cache\"\\` nested within \\`unstable_cache\\`. Root params are not available in this context.",
"1141": "Route %s used \\`import('next/root-params').%s()\\` inside \\`unstable_cache\\`. This is not supported. Use \\`\"use cache\"\\` instead." "1141": "Route %s used \\`import('next/root-params').%s()\\` inside \\`unstable_cache\\`. This is not supported. Use \\`\"use cache\"\\` instead.",
"1142": "Route %s used \\`import('next/root-params').%s()\\` inside \\`generateStaticParams\\`, but the \\`%s\\` parameter was not provided by a parent \\`generateStaticParams\\`. In \\`generateStaticParams\\`, root params are only available for segments nested below the segment that provides them."
} }
...@@ -109,8 +109,15 @@ export function getRootParam(paramName: string): Promise<ParamValue> { ...@@ -109,8 +109,15 @@ export function getRootParam(paramName: string): Promise<ParamValue> {
break break
} }
case 'private-cache': case 'private-cache':
case 'prerender-runtime': case 'prerender-runtime': {
break
}
case 'generate-static-params': { case 'generate-static-params': {
if (!(paramName in workUnitStore.rootParams)) {
throw new Error(
`Route ${workStore.route} used ${apiName} inside \`generateStaticParams\`, but the \`${paramName}\` parameter was not provided by a parent \`generateStaticParams\`. In \`generateStaticParams\`, root params are only available for segments nested below the segment that provides them.`
)
}
break break
} }
default: { default: {
......
import { lang } from 'next/root-params'
import { ReactNode } from 'react'
export default async function Root({ children }: { children: ReactNode }) {
return (
<html lang={await lang()}>
<body>{children}</body>
</html>
)
}
export async function generateStaticParams() {
// This should error: we're reading `lang` inside the generateStaticParams
// that is supposed to define it. The `lang` param is not yet available
// because no parent segment has provided it.
const l = await lang()
return [{ lang: l, locale: 'us' }]
}
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
experimental: {
rootParams: true,
},
}
export default nextConfig
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."
)
})
}
})
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