From dadbc5e73d9199f56c43db2e2b397239fe2d4e45 Mon Sep 17 00:00:00 2001 From: Zack Tanner <1939140+ztanner@users.noreply.github.com> Date: Tue, 27 Aug 2024 13:26:51 -0700 Subject: [PATCH] refactor path creation in walkTreeWithFlightRouterState --- .../walk-tree-with-flight-router-state.tsx | 92 +++++++++---------- 1 file changed, 44 insertions(+), 48 deletions(-) diff --git a/packages/next/src/server/app-render/walk-tree-with-flight-router-state.tsx b/packages/next/src/server/app-render/walk-tree-with-flight-router-state.tsx index e88b40316582f..08d5aac9e0adc 100644 --- a/packages/next/src/server/app-render/walk-tree-with-flight-router-state.tsx +++ b/packages/next/src/server/app-render/walk-tree-with-flight-router-state.tsx @@ -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 }