Skip to content

Commit 3d9dc30

Browse files
committed
fix: handle review comments
1 parent b042250 commit 3d9dc30

File tree

11 files changed

+51
-29
lines changed

11 files changed

+51
-29
lines changed

packages/next/src/build/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3480,7 +3480,9 @@ export default async function build(
34803480
route.fallbackRouteParams &&
34813481
route.fallbackRouteParams.length > 0 &&
34823482
// If all the fallback route params are parallel route
3483-
// params, then we still consider it a static route.
3483+
// params, then we still consider it a static route. This is
3484+
// required to ensure that the changes are backwards
3485+
// compatible with the previous builder implementations.
34843486
!route.fallbackRouteParams.every(
34853487
(param) => param.isParallelRouteParam
34863488
)

packages/next/src/build/static-paths/app.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import type { IncrementalCache } from '../../server/lib/incremental-cache'
1919
import {
2020
normalizePathname,
2121
encodeParam,
22-
filterNonParallelFallbackRouteParams,
2322
createFallbackRouteParam,
2423
} from './utils'
2524
import escapePathDelimiters from '../../shared/lib/router/utils/escape-path-delimiters'
@@ -504,7 +503,9 @@ export function assignErrorIfEmpty(
504503
// calculation because parallel route params won't contribute to
505504
// the request pathname.
506505
r.fallbackRouteParams
507-
? filterNonParallelFallbackRouteParams(r.fallbackRouteParams).length
506+
? r.fallbackRouteParams.filter(
507+
(param) => !param.isParallelRouteParam
508+
).length
508509
: 0
509510
)
510511
}
@@ -529,8 +530,9 @@ export function assignErrorIfEmpty(
529530
// We only consider non-parallel fallback route params for this
530531
// calculation because parallel route params won't contribute to
531532
// the request pathname.
532-
filterNonParallelFallbackRouteParams(route.fallbackRouteParams)
533-
.length > minFallbacks)
533+
route.fallbackRouteParams.filter(
534+
(param) => !param.isParallelRouteParam
535+
).length > minFallbacks)
534536
) {
535537
route.throwOnEmptyStaticShell = false // Should not throw on empty static shell.
536538
} else {
@@ -921,9 +923,8 @@ export async function buildAppStaticPaths({
921923
const fallbackRootParams: string[] = []
922924
// Only add the param to the fallback root params if it's not a
923925
// parallel route param. They won't contribute to the request pathname.
924-
for (const { paramName } of filterNonParallelFallbackRouteParams(
925-
fallbackRouteParams
926-
)) {
926+
for (const { paramName, isParallelRouteParam } of fallbackRouteParams) {
927+
if (isParallelRouteParam) continue
927928
if (rootParamSet.has(paramName)) {
928929
fallbackRootParams.push(paramName)
929930
}

packages/next/src/build/static-paths/utils.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,3 @@ export function createFallbackRouteParam(
4545
): FallbackRouteParam {
4646
return { paramName, isParallelRouteParam }
4747
}
48-
49-
/**
50-
* Filters out all the parallel route params from the fallback route params.
51-
*
52-
* @param fallbackRouteParams - The fallback route params to filter.
53-
* @returns The filtered fallback route params.
54-
*/
55-
export function filterNonParallelFallbackRouteParams(
56-
fallbackRouteParams: readonly FallbackRouteParam[]
57-
): readonly FallbackRouteParam[] {
58-
return fallbackRouteParams.filter((param) => !param.isParallelRouteParam)
59-
}

packages/next/src/client/components/navigation-untracked.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ function hasFallbackRouteParams(): boolean {
2121
case 'prerender-client':
2222
case 'prerender-ppr':
2323
const fallbackParams = workUnitStore.fallbackRouteParams
24+
// We only consider the non-parallel fallback route params for this
25+
// check because parallel route params won't contribute to the request
26+
// pathname.
2427
return fallbackParams ? fallbackParams.sizes.route > 0 : false
2528
case 'prerender-legacy':
2629
case 'request':

packages/next/src/export/routes/app-page.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ export async function exportAppPage(
139139
// means that the route has unknown route params.
140140
if (
141141
!fallbackRouteParams ||
142+
// We only consider the non-parallel fallback route params to maintain
143+
// backwards compatibility with the previous implementation.
142144
fallbackRouteParams.sizes.route === 0 ||
143145
renderOpts.experimental.clientParamParsing
144146
) {

packages/next/src/server/app-render/app-render.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3910,7 +3910,11 @@ async function prerenderToStream(
39103910
// move the fallback route params out of the flight router state, we need
39113911
// to always perform a dynamic resume after the static prerender.
39123912
const hasFallbackRouteParams =
3913-
fallbackRouteParams && fallbackRouteParams.sizes.route > 0
3913+
// We consider both the route and parallel fallback route params for
3914+
// this check because we're determining if we can emit a completely
3915+
// static HTML prerender. If any of the route params are unknown then
3916+
// the prerender will be dynamic.
3917+
fallbackRouteParams && fallbackRouteParams.sizes.total > 0
39143918

39153919
if (serverIsDynamic || hasFallbackRouteParams) {
39163920
// Dynamic case

packages/next/src/server/app-render/dynamic-rendering.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,10 @@ export function useDynamicRouteParams(expression: string) {
595595
case 'prerender-client':
596596
case 'prerender': {
597597
const fallbackParams = workUnitStore.fallbackRouteParams
598-
if (fallbackParams && fallbackParams.sizes.route > 0) {
598+
// We consider both the route and parallel fallback route params for
599+
// this check because `useParams()` will also return the parallel
600+
// fallback route params.
601+
if (fallbackParams && fallbackParams.sizes.total > 0) {
599602
// We are in a prerender with cacheComponents semantics. We are going to
600603
// hang here and never resolve. This will cause the currently
601604
// rendering component to effectively be a dynamic hole.
@@ -611,7 +614,10 @@ export function useDynamicRouteParams(expression: string) {
611614
}
612615
case 'prerender-ppr': {
613616
const fallbackParams = workUnitStore.fallbackRouteParams
614-
if (fallbackParams && fallbackParams.sizes.route > 0) {
617+
// We consider both the route and parallel fallback route params for
618+
// this check because `useParams()` will also return the parallel
619+
// fallback route params.
620+
if (fallbackParams && fallbackParams.sizes.total > 0) {
615621
return postponeWithTracking(
616622
workStore.route,
617623
expression,

packages/next/src/server/app-render/postponed-state.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ export async function getDynamicHTMLPostponedState(
8080
const data: DynamicHTMLPostponedState['data'] = [preludeState, postponed]
8181
const dataString = JSON.stringify(data)
8282

83-
if (!fallbackRouteParams || fallbackRouteParams.sizes.route === 0) {
83+
// If there are no fallback route params, we can just serialize the postponed
84+
// state as is. We consider both the route and parallel fallback route params
85+
// for this check because they both contribute to the outputted postponed
86+
// state.
87+
if (!fallbackRouteParams || fallbackRouteParams.sizes.total === 0) {
8488
// Serialized as `<postponedString.length>:<postponedString><renderResumeDataCache>`
8589
return `${dataString.length}:${dataString}${await stringifyResumeDataCache(
8690
createRenderResumeDataCache(resumeDataCache)

packages/next/src/server/lib/implicit-tags.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ export async function getImplicitTags(
8080
fallbackRouteParams: null | OpaqueFallbackRouteParams
8181
): Promise<ImplicitTags> {
8282
const tags: string[] = []
83+
84+
// As we're using this to determine if the route pathname is dynamic, we only
85+
// consider the route fallback route params for this check.
8386
const hasFallbackRouteParams =
8487
fallbackRouteParams && fallbackRouteParams.sizes.route > 0
8588

packages/next/src/server/request/fallback-params.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export type OpaqueFallbackRouteParams = {
3636
* the route should be considered dynamic.
3737
*/
3838
readonly parallel: number
39+
40+
/**
41+
* The total number of fallback route params.
42+
*/
43+
readonly total: number
3944
}
4045

4146
/**
@@ -83,14 +88,15 @@ export function createOpaqueFallbackRouteParams(
8388
// be also be unique.
8489
const uniqueID = Math.random().toString(16).slice(2)
8590

86-
const sizes = { route: 0, parallel: 0 }
91+
const sizes = { route: 0, parallel: 0, total: 0 }
8792
const keys = new Map<string, string>()
8893

8994
for (const { paramName, isParallelRouteParam } of fallbackRouteParams) {
9095
// We need to track the sizes of the fallback route params to determine if
9196
// the render should be halted during static generation.
9297
if (isParallelRouteParam) sizes.parallel++
9398
else sizes.route++
99+
sizes.total++
94100

95101
// Generate a unique key for the fallback route param, if this key is found
96102
// in the static output, it represents a bug in cache components.
@@ -102,10 +108,7 @@ export function createOpaqueFallbackRouteParams(
102108
has: keys.has.bind(keys),
103109
get: keys.get.bind(keys),
104110
*[Symbol.iterator](): IterableIterator<[string, string]> {
105-
for (const { paramName, isParallelRouteParam } of fallbackRouteParams) {
106-
// We only want to include the route segments, not the parallel route
107-
// segments.
108-
if (isParallelRouteParam) continue
111+
for (const { paramName } of fallbackRouteParams) {
109112
yield [paramName, keys.get(paramName)!]
110113
}
111114
},

0 commit comments

Comments
 (0)