Fix: Throw an error for empty array return in generateStaticParams
with output:export
(#57053)
Fixes #57038 # What? Added an error message when `generateStaticParams` returns an empty array with `output:export`. # Why? To provide developers with clear feedback when `generateStaticParams` is not used correctly. # How? Modified the condition checks around the use of `generateStaticParams` to include a check for an empty array and added a corresponding error message. --------- Co-authored-by: Steven <steven@ceriously.com>
This commit is contained in:
parent
aa48b65f9a
commit
91dba7d125
5 changed files with 44 additions and 10 deletions
|
@ -1897,15 +1897,19 @@ export default async function build(
|
|||
const isDynamic = isDynamicRoute(page)
|
||||
const hasGenerateStaticParams =
|
||||
!!workerResult.prerenderRoutes?.length
|
||||
const isEmptyGenerateStaticParams =
|
||||
workerResult.prerenderRoutes?.length === 0
|
||||
|
||||
if (
|
||||
config.output === 'export' &&
|
||||
isDynamic &&
|
||||
!hasGenerateStaticParams
|
||||
) {
|
||||
throw new Error(
|
||||
`Page "${page}" is missing "generateStaticParams()" so it cannot be used with "output: export" config.`
|
||||
)
|
||||
if (config.output === 'export' && isDynamic) {
|
||||
if (isEmptyGenerateStaticParams) {
|
||||
throw new Error(
|
||||
`Page "${page}"'s "generateStaticParams()" returned an empty array, which is not allowed with "output: export" config.`
|
||||
)
|
||||
} else if (!hasGenerateStaticParams) {
|
||||
throw new Error(
|
||||
`Page "${page}" is missing "generateStaticParams()" so it cannot be used with "output: export" config.`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
|
|
|
@ -1794,7 +1794,12 @@ export default abstract class Server<ServerOptions extends Options = Options> {
|
|||
)
|
||||
}
|
||||
const resolvedWithoutSlash = removeTrailingSlash(resolvedUrlPathname)
|
||||
if (!staticPaths?.includes(resolvedWithoutSlash)) {
|
||||
if (!staticPaths || staticPaths.length === 0) {
|
||||
throw new Error(
|
||||
`Page "${page}"'s "generateStaticParams()" returned an empty array, which is not allowed with "output: export" config.`
|
||||
)
|
||||
}
|
||||
if (!staticPaths.includes(resolvedWithoutSlash)) {
|
||||
throw new Error(
|
||||
`Page "${page}" is missing param "${resolvedWithoutSlash}" in "generateStaticParams()", which is required with "output: export" config.`
|
||||
)
|
||||
|
|
|
@ -21,5 +21,15 @@ describe('app dir - with output export - dynamic missing gsp dev', () => {
|
|||
'Page "/another/[slug]/page" cannot use both "use client" and export function "generateStaticParams()".',
|
||||
})
|
||||
})
|
||||
|
||||
it('should error when generateStaticParams returns an empty array', async () => {
|
||||
await runTests({
|
||||
isDev: true,
|
||||
dynamicPage: 'undefined',
|
||||
generateStaticParamsOpt: 'set empty',
|
||||
expectedErrMsg:
|
||||
'Page "/another/[slug]/page"\'s "generateStaticParams()" returned an empty array, which is not allowed with "output: export" config.',
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -21,5 +21,15 @@ describe('app dir - with output export - dynamic missing gsp prod', () => {
|
|||
'Page "/another/[slug]/page" cannot use both "use client" and export function "generateStaticParams()".',
|
||||
})
|
||||
})
|
||||
|
||||
it('should error when generateStaticParams returns an empty array', async () => {
|
||||
await runTests({
|
||||
isDev: false,
|
||||
dynamicPage: 'undefined',
|
||||
generateStaticParamsOpt: 'set empty',
|
||||
expectedErrMsg:
|
||||
'Page "/another/[slug]"\'s "generateStaticParams()" returned an empty array, which is not allowed with "output: export" config.',
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -99,7 +99,7 @@ export async function runTests({
|
|||
trailingSlash?: boolean
|
||||
dynamicPage?: string
|
||||
dynamicApiRoute?: string
|
||||
generateStaticParamsOpt?: 'set noop' | 'set client'
|
||||
generateStaticParamsOpt?: 'set noop' | 'set client' | 'set empty'
|
||||
expectedErrMsg?: string
|
||||
}) {
|
||||
if (trailingSlash !== undefined) {
|
||||
|
@ -124,6 +124,11 @@ export async function runTests({
|
|||
slugPage.replace('export function generateStaticParams', 'function noop')
|
||||
} else if (generateStaticParamsOpt === 'set client') {
|
||||
slugPage.prepend('"use client"\n')
|
||||
} else if (generateStaticParamsOpt === 'set empty') {
|
||||
slugPage.replace(
|
||||
"return [{ slug: 'first' }, { slug: 'second' }]",
|
||||
'return []'
|
||||
)
|
||||
}
|
||||
await fs.remove(distDir)
|
||||
await fs.remove(exportDir)
|
||||
|
|
Loading…
Reference in a new issue