From 9abdb6ae2499eeddb08b25a373702f7765d4265f Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 8 Aug 2023 23:35:18 +0200 Subject: [PATCH] refactor --- .../next/src/server/app-render/app-render.tsx | 86 ++++++++----------- 1 file changed, 38 insertions(+), 48 deletions(-) diff --git a/packages/next/src/server/app-render/app-render.tsx b/packages/next/src/server/app-render/app-render.tsx index f2390b3dfc13..8abf83922776 100644 --- a/packages/next/src/server/app-render/app-render.tsx +++ b/packages/next/src/server/app-render/app-render.tsx @@ -788,34 +788,20 @@ export async function renderToHTMLOrFlight( : [actualSegment, parallelRouteKey] const parallelRoute = parallelRoutes[parallelRouteKey] - const childSegment = parallelRoute[0] const childSegmentParam = getDynamicParamFromSegment(childSegment) + const notFound = NotFound ? : undefined - // if we're prefetching and that there's a Loading component, we bail out - // otherwise we keep rendering for the prefetch. - // We also want to bail out if there's no Loading component in the tree. - if ( - isPrefetch && - (Loading || !hasLoadingComponentInTree(parallelRoute)) - ) { - const childProp: ChildProp = { - // Null indicates the tree is not fully rendered - current: null, - segment: addSearchParamsIfPageSegment( - childSegmentParam - ? childSegmentParam.treeSegment - : childSegment, - query - ), - } - - // This is turned back into an object below. + function createParallelRouteMapItem( + childProp: ChildProp, + segmentPath: FlightDataPath, + styles?: React.ReactNode + ): [string, React.ReactNode] { return [ parallelRouteKey, : undefined} loadingStyles={loadingStyles} hasLoading={Boolean(Loading)} @@ -827,13 +813,38 @@ export async function renderToHTMLOrFlight( } templateStyles={templateStyles} - notFound={NotFound ? : undefined} + notFound={notFound} notFoundStyles={notFoundStyles} childProp={childProp} + styles={styles} />, ] } + // if we're prefetching and that there's a Loading component, we bail out + // otherwise we keep rendering for the prefetch. + // We also want to bail out if there's no Loading component in the tree. + if ( + isPrefetch && + (Loading || !hasLoadingComponentInTree(parallelRoute)) + ) { + const childProp: ChildProp = { + // Null indicates the tree is not fully rendered + current: null, + segment: addSearchParamsIfPageSegment( + childSegmentParam + ? childSegmentParam.treeSegment + : childSegment, + query + ), + } + + return createParallelRouteMapItem( + childProp, + createSegmentPath(currentSegmentPath) + ) + } + // Create the child component const { Component: ChildComponent, styles: childStyles } = await createComponentTree({ @@ -860,32 +871,11 @@ export async function renderToHTMLOrFlight( ), } - const segmentPath = createSegmentPath(currentSegmentPath) - - // This is turned back into an object below. - return [ - parallelRouteKey, - : undefined} - loadingStyles={loadingStyles} - // TODO-APP: Add test for loading returning `undefined`. This currently can't be tested as the `webdriver()` tab will wait for the full page to load before returning. - hasLoading={Boolean(Loading)} - template={ - - } - templateStyles={templateStyles} - notFound={NotFound ? : undefined} - notFoundStyles={notFoundStyles} - childProp={childProp} - styles={childStyles} - />, - ] + return createParallelRouteMapItem( + childProp, + createSegmentPath(currentSegmentPath), + childStyles + ) } ) )