Skip to content

Commit 526ab5e

Browse files
authored
feat: add fallback root params support for prefetch segment data routes (#82282)
### What? Add support for generating prefetch segment data routes for pages with fallback root parameters. ### Why? When routes have fallback root parameters but no segment paths, the prefetch system wasn't generating the necessary rewrite rules. This caused client-side navigation to fail for these routes because the `/_tree` requests couldn't be properly rewritten to the prefetch RSC route. ### How? - Add `hasFallbackRootParams` flag and `prefetchSegmentDataRoutes` to `ManifestRoute` type - Generate inverse prefetch segment data routes for `/_tree` path when routes have fallback root params - Set flag to simplify route regex matching for these routes Related Pull Request: vercel/vercel#13653
1 parent b1f80c1 commit 526ab5e

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

packages/next/src/build/index.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ import { InvariantError } from '../shared/lib/invariant-error'
191191
import { HTML_LIMITED_BOT_UA_RE_STRING } from '../shared/lib/router/utils/is-bot'
192192
import type { UseCacheTrackerKey } from './webpack/plugins/telemetry-plugin/use-cache-tracker-utils'
193193
import {
194+
buildInversePrefetchSegmentDataRoute,
194195
buildPrefetchSegmentDataRoute,
195196
type PrefetchSegmentDataRoute,
196197
} from '../server/lib/router-utils/build-prefetch-segment-data-route'
@@ -401,6 +402,18 @@ export type ManifestRoute = ManifestBuiltRoute & {
401402
page: string
402403
namedRegex?: string
403404
routeKeys?: { [key: string]: string }
405+
406+
/**
407+
* If true, this indicates that the route has fallback root params. This is
408+
* used to simplify the route regex for matching.
409+
*/
410+
hasFallbackRootParams?: boolean
411+
412+
/**
413+
* The prefetch segment data routes for this route. This is used to rewrite
414+
* the prefetch segment data routes (or the inverse) to the correct
415+
* destination.
416+
*/
404417
prefetchSegmentDataRoutes?: PrefetchSegmentDataRoute[]
405418

406419
/**
@@ -3172,7 +3185,12 @@ export default async function build(
31723185
}
31733186
}
31743187

3175-
if (!isAppRouteHandler && metadata?.segmentPaths) {
3188+
if (
3189+
!isAppRouteHandler &&
3190+
(metadata?.segmentPaths ||
3191+
(route.fallbackRootParams &&
3192+
route.fallbackRootParams.length > 0))
3193+
) {
31763194
// If PPR isn't enabled, then we might not find the dynamic
31773195
// route by pathname. If that's the case, we need to find the
31783196
// route by page.
@@ -3185,7 +3203,7 @@ export default async function build(
31853203
}
31863204
}
31873205

3188-
if (metadata.segmentPaths) {
3206+
if (metadata?.segmentPaths) {
31893207
const pageSegmentPath = metadata.segmentPaths.find((item) =>
31903208
item.endsWith('__PAGE__')
31913209
)
@@ -3216,6 +3234,27 @@ export default async function build(
32163234
builtSegmentDataRoute
32173235
)
32183236
}
3237+
// If the route has fallback root params, and we don't have
3238+
// any segment paths, we need to write the inverse prefetch
3239+
// segment data route so that it can first rewrite the /_tree
3240+
// request to the prefetch RSC route. We also need to set the
3241+
// `hasFallbackRootParams` flag so that we can simplify the
3242+
// route regex for matching.
3243+
else if (
3244+
route.fallbackRootParams &&
3245+
route.fallbackRootParams.length > 0
3246+
) {
3247+
dynamicRoute.hasFallbackRootParams = true
3248+
dynamicRoute.prefetchSegmentDataRoutes = [
3249+
buildInversePrefetchSegmentDataRoute(
3250+
dynamicRoute.page,
3251+
// We use the special segment path of `/_tree` because it's
3252+
// the first one sent by the client router so it's the only
3253+
// one we need to rewrite to the regular prefetch RSC route.
3254+
'/_tree'
3255+
),
3256+
]
3257+
}
32193258
}
32203259

32213260
pageInfos.set(route.pathname, {

0 commit comments

Comments
 (0)