Skip to content

Commit

Permalink
refactor path creation in walkTreeWithFlightRouterState (#69383)
Browse files Browse the repository at this point in the history
There's no reason to use `map` here, since it's making the code harder
to read and we ultimately have to flatten & filter out falsey values.

This updates the path construction to use a `for..of` and only pushes
paths that are relevant. This also removes the nested path array that
was getting created every iteration of the loop.
  • Loading branch information
ztanner committed Aug 28, 2024
1 parent cfab473 commit c1a7463
Showing 1 changed file with 44 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,55 +184,51 @@ export async function walkTreeWithFlightRouterState({
)
}

// Walk through all parallel routes.
const paths: FlightDataPath[] = (
await Promise.all(
parallelRoutesKeys.map(async (parallelRouteKey) => {
// for (const parallelRouteKey of parallelRoutesKeys) {
const parallelRoute = parallelRoutes[parallelRouteKey]

const currentSegmentPath: FlightSegmentPath = isFirst
? [parallelRouteKey]
: [actualSegment, parallelRouteKey]
const paths: FlightDataPath[] = []

const path = await walkTreeWithFlightRouterState({
ctx,
createSegmentPath: (child) => {
return createSegmentPath([...currentSegmentPath, ...child])
},
loaderTreeToFilter: parallelRoute,
parentParams: currentParams,
flightRouterState:
flightRouterState && flightRouterState[1][parallelRouteKey],
parentRendered: parentRendered || renderComponentsOnThisLevel,
isFirst: false,
rscPayloadHead,
injectedCSS: injectedCSSWithCurrentLayout,
injectedJS: injectedJSWithCurrentLayout,
injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout,
rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove,
getMetadataReady,
preloadCallbacks,
})

return path
.map((item) => {
// we don't need to send over default routes in the flight data
// because they are always ignored by the client, unless it's a refetch
if (
item[0] === DEFAULT_SEGMENT_KEY &&
flightRouterState &&
!!flightRouterState[1][parallelRouteKey][0] &&
flightRouterState[1][parallelRouteKey][3] !== 'refetch'
) {
return null
}
return [actualSegment, parallelRouteKey, ...item]
})
.filter(Boolean) as FlightDataPath[]
})
)
).flat()
// Walk through all parallel routes.
for (const parallelRouteKey of parallelRoutesKeys) {
const parallelRoute = parallelRoutes[parallelRouteKey]

const currentSegmentPath: FlightSegmentPath = isFirst
? [parallelRouteKey]
: [actualSegment, parallelRouteKey]

const subPaths = await walkTreeWithFlightRouterState({
ctx,
createSegmentPath: (child) => {
return createSegmentPath([...currentSegmentPath, ...child])
},
loaderTreeToFilter: parallelRoute,
parentParams: currentParams,
flightRouterState:
flightRouterState && flightRouterState[1][parallelRouteKey],
parentRendered: parentRendered || renderComponentsOnThisLevel,
isFirst: false,
rscPayloadHead,
injectedCSS: injectedCSSWithCurrentLayout,
injectedJS: injectedJSWithCurrentLayout,
injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout,
rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove,
getMetadataReady,
preloadCallbacks,
})

for (const subPath of subPaths) {
// we don't need to send over default routes in the flight data
// because they are always ignored by the client, unless it's a refetch
if (
subPath[0] === DEFAULT_SEGMENT_KEY &&
flightRouterState &&
!!flightRouterState[1][parallelRouteKey][0] &&
flightRouterState[1][parallelRouteKey][3] !== 'refetch'
) {
continue
}

paths.push([actualSegment, parallelRouteKey, ...subPath])
}
}

return paths
}

0 comments on commit c1a7463

Please sign in to comment.