diff --git a/packages/next/errors.json b/packages/next/errors.json index d4e19f0cb8..a7297d7c0c 100644 --- a/packages/next/errors.json +++ b/packages/next/errors.json @@ -1447,5 +1447,7 @@ "1446": "Missing initURL", "1447": "Could not determine origin for forwarded Server Actions request. This can happen if port or hostname are not configured for this server.", "1448": "Error in the \"%s\" app-route HMR subscription", - "1449": "Accessed `searchParams` during prerendering." + "1449": "Accessed `searchParams` during prerendering.", + "1450": "Invalid value returned from generateStaticParams for \"%s\". Expected an array, but received type %s. See more info here: https://nextjs.org/docs/messages/generate-static-params", + "1451": "Invalid value at index %s returned from generateStaticParams for \"%s\". Expected an object, but received type %s. See more info here: https://nextjs.org/docs/messages/generate-static-params" } diff --git a/packages/next/src/build/static-paths/app.ts b/packages/next/src/build/static-paths/app.ts index 4e0c84ebc9..a9c35e8853 100644 --- a/packages/next/src/build/static-paths/app.ts +++ b/packages/next/src/build/static-paths/app.ts @@ -31,6 +31,7 @@ import { throwEmptyGenerateStaticParamsError } from '../../shared/lib/errors/emp import type { AppRouteModule } from '../../server/route-modules/app-route/module.compiled' import type { NormalizedAppRoute } from '../../shared/lib/router/routes/app' import { interceptionPrefixFromParamType } from '../../shared/lib/router/utils/interception-prefix-from-param-type' +import { isPlainObject } from '../../shared/lib/is-plain-object' import { type GenerateStaticParamsStore, workUnitAsyncStorage, @@ -598,11 +599,18 @@ export function assignStaticShellMetadata( } } +function getValueType(value: unknown): string { + if (value === null) return 'null' + if (Array.isArray(value)) return 'array' + return typeof value +} + /** * Calls a single generateStaticParams function within a WorkUnitStore context, * making root param getters available during static param generation. */ async function callGenerateStaticParams( + page: string, generateStaticParams: NonNullable, parentParams: Params, rootParamKeys: readonly string[], @@ -622,9 +630,27 @@ async function callGenerateStaticParams( rootParams, } - return workUnitAsyncStorage.run(workUnitStore, generateStaticParams, { - params: parentParams, - }) + const generatedParams: unknown = await workUnitAsyncStorage.run( + workUnitStore, + generateStaticParams, + { params: parentParams } + ) + + if (!Array.isArray(generatedParams)) { + throw new Error( + `Invalid value returned from generateStaticParams for "${page}". Expected an array, but received type ${getValueType(generatedParams)}. See more info here: https://nextjs.org/docs/messages/generate-static-params` + ) + } + + for (const [index, params] of generatedParams.entries()) { + if (!isPlainObject(params)) { + throw new Error( + `Invalid value at index ${index} returned from generateStaticParams for "${page}". Expected an object, but received type ${getValueType(params)}. See more info here: https://nextjs.org/docs/messages/generate-static-params` + ) + } + } + + return generatedParams } /** @@ -695,6 +721,7 @@ export async function generateRouteStaticParams( // Process each parent parameter combination for (const parentParams of params) { const result = await callGenerateStaticParams( + store.page, current.generateStaticParams, parentParams, rootParamKeys, @@ -716,6 +743,7 @@ export async function generateRouteStaticParams( } else { // No parent params, call generateStaticParams with empty object const result = await callGenerateStaticParams( + store.page, current.generateStaticParams, {}, rootParamKeys,