Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Aug 8, 2023
1 parent 3fdb847 commit 9abdb6a
Showing 1 changed file with 38 additions and 48 deletions.
86 changes: 38 additions & 48 deletions packages/next/src/server/app-render/app-render.tsx
Expand Up @@ -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 ? <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,
<LayoutRouter
parallelRouterKey={parallelRouteKey}
segmentPath={createSegmentPath(currentSegmentPath)}
segmentPath={segmentPath}
loading={Loading ? <Loading /> : undefined}
loadingStyles={loadingStyles}
hasLoading={Boolean(Loading)}
Expand All @@ -827,13 +813,38 @@ export async function renderToHTMLOrFlight(
</Template>
}
templateStyles={templateStyles}
notFound={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({
Expand All @@ -860,32 +871,11 @@ export async function renderToHTMLOrFlight(
),
}

const segmentPath = createSegmentPath(currentSegmentPath)

// This is turned back into an object below.
return [
parallelRouteKey,
<LayoutRouter
parallelRouterKey={parallelRouteKey}
segmentPath={segmentPath}
error={ErrorComponent}
errorStyles={errorStyles}
loading={Loading ? <Loading /> : 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={
<Template>
<RenderFromTemplateContext />
</Template>
}
templateStyles={templateStyles}
notFound={NotFound ? <NotFound /> : undefined}
notFoundStyles={notFoundStyles}
childProp={childProp}
styles={childStyles}
/>,
]
return createParallelRouteMapItem(
childProp,
createSegmentPath(currentSegmentPath),
childStyles
)
}
)
)
Expand Down

0 comments on commit 9abdb6a

Please sign in to comment.