Unverified Commit 6b9af3e0 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix fallback detection logic when multiple generateStaticParams are needed (#47982)

### What?

Our current logic of detecting if a route allows dynamic params or not
(`fallback`) is flawed, and this PR fixes it.

### Why?

Right now, if no `generateStaticParams` is specified we return
`fallback: undefined` during dev. However, for an app with multiple
params, it may have multiple `generateStaticParams` defined in different
levels. If some level isn't covered by any `generateStaticParams`, we
still can't determine the fallback value.

### How?

I added a naive implementation to check if all params are covered by
`generateStaticParams` in the current or inner layers.

Closes NEXT-946
parent 98dfbc07
......@@ -1086,6 +1086,7 @@ export type AppConfig = {
}
export type GenerateParams = Array<{
config?: AppConfig
isDynamicSegment?: boolean
segmentPath: string
getStaticPaths?: GetStaticPaths
generateStaticParams?: any
......@@ -1133,9 +1134,11 @@ export const collectGenerateParams = async (
const config = collectAppConfig(mod)
const isClientComponent = isClientReference(mod)
const isDynamicSegment = segment[0] && /^\[.+\]$/.test(segment[0])
const result = {
isLayout,
isDynamicSegment,
segmentPath: `/${parentSegments.join('/')}${
segment[0] && parentSegments.length > 0 ? '/' : ''
}${segment[0]}`,
......@@ -1152,7 +1155,11 @@ export const collectGenerateParams = async (
if (result.config || result.generateStaticParams || result.getStaticPaths) {
generateParams.push(result)
} else if (isDynamicSegment) {
// It is a dynamic route, but no config was provided
generateParams.push(result)
}
return collectGenerateParams(
segment[1]?.children,
parentSegments,
......@@ -1243,7 +1250,7 @@ export async function buildAppStaticPaths({
// if generateStaticParams is being used we iterate over them
// collecting them from each level
type Params = Array<Record<string, string | string[]>>
let hadGenerateParams = false
let hadAllParamsGenerated = false
const buildParams = async (
paramsItems: Params = [{}],
......@@ -1258,9 +1265,15 @@ export async function buildAppStaticPaths({
typeof curGenerate.generateStaticParams !== 'function' &&
idx < generateParams.length
) {
if (curGenerate.isDynamicSegment) {
// This dynamic level has no generateStaticParams so we change
// this flag to false, but it could be covered by a later
// generateStaticParams so it could be set back to true.
hadAllParamsGenerated = false
}
return buildParams(paramsItems, idx + 1)
}
hadGenerateParams = true
hadAllParamsGenerated = true
const newParams = []
......@@ -1287,7 +1300,7 @@ export async function buildAppStaticPaths({
(generate) => generate.config?.dynamicParams === false
)
if (!hadGenerateParams) {
if (!hadAllParamsGenerated) {
return {
paths: undefined,
fallback:
......
......@@ -54,6 +54,16 @@ createNextDescribe(
'A required parameter (slug) was not provided as a string received object'
)
})
it('should correctly handle multi-level generateStaticParams when some levels are missing', async () => {
const browser = await next.browser('/flight/foo/bar')
const v = ~~(Math.random() * 1000)
await browser.eval(`document.cookie = "test-cookie=${v}"`)
await browser.elementByCss('button').click()
await check(async () => {
return await browser.elementByCss('h1').text()
}, v.toString())
})
}
it('should correctly skip caching POST fetch for POST handler', async () => {
......@@ -216,6 +226,7 @@ createNextDescribe(
'dynamic-error/[id]/page.js',
'dynamic-no-gen-params-ssr/[slug]/page.js',
'dynamic-no-gen-params/[slug]/page.js',
'flight/[slug]/[slug2]/page.js',
'force-dynamic-catch-all/[slug]/[[...id]]/page.js',
'force-dynamic-no-prerender/[id]/page.js',
'force-dynamic-prerender/[slug]/page.js',
......@@ -591,16 +602,6 @@ createNextDescribe(
'^\\/partial\\-gen\\-params\\-no\\-additional\\-slug\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$'
),
},
'/partial-gen-params/[lang]/[slug]': {
dataRoute: '/partial-gen-params/[lang]/[slug].rsc',
dataRouteRegex: normalizeRegEx(
'^\\/partial\\-gen\\-params\\/([^\\/]+?)\\/([^\\/]+?)\\.rsc$'
),
fallback: null,
routeRegex: normalizeRegEx(
'^\\/partial\\-gen\\-params\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$'
),
},
'/force-static/[slug]': {
dataRoute: '/force-static/[slug].rsc',
dataRouteRegex: normalizeRegEx(
......
import { cookies } from 'next/headers'
import Refresh from './refresh'
export default function Page() {
const cookieValue = cookies().get('test-cookie')
return (
<>
<Refresh />
<h1>{cookieValue?.value}</h1>
</>
)
}
'use client'
import { useRouter } from 'next/navigation'
export default function Refresh() {
const router = useRouter()
return <button onClick={() => router.refresh()}>Refresh</button>
}
export default function Layout({ children }) {
return children
}
export function generateStaticParams() {
return [{ slug: 'foo' }]
}
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