Unverified Commit 73217b56 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Refactor layout router creation in app-render (#54126)

Merge the rendering of `<LayoutRouter>` component into one place, with different props in different branches.
parent 662dcedd
......@@ -825,24 +825,10 @@ export async function renderToHTMLOrFlight(
const notFoundComponent =
NotFound && isChildrenRouteKey ? <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
),
}
function getParallelRoutePair(
currentChildProp: ChildProp,
currentStyles: React.ReactNode
): [string, React.ReactNode] {
// This is turned back into an object below.
return [
parallelRouteKey,
......@@ -851,6 +837,7 @@ export async function renderToHTMLOrFlight(
segmentPath={createSegmentPath(currentSegmentPath)}
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)}
error={ErrorComponent}
errorStyles={errorStyles}
......@@ -862,14 +849,32 @@ export async function renderToHTMLOrFlight(
templateStyles={templateStyles}
notFound={notFoundComponent}
notFoundStyles={notFoundStyles}
childProp={childProp}
childProp={currentChildProp}
styles={currentStyles}
/>,
]
}
// Create the child component
const { Component: ChildComponent, styles: childStyles } =
await createComponentTree({
// 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.
let currentStyles = undefined
let childElement = null
const childPropSegment = addSearchParamsIfPageSegment(
childSegmentParam ? childSegmentParam.treeSegment : childSegment,
query
)
if (
!(
isPrefetch &&
(Loading || !hasLoadingComponentInTree(parallelRoute))
)
) {
// Create the child component
const {
Component: ChildComponent,
styles: childComponentStyles,
} = await createComponentTree({
createSegmentPath: (child) => {
return createSegmentPath([...currentSegmentPath, ...child])
},
......@@ -883,42 +888,16 @@ export async function renderToHTMLOrFlight(
metadataOutlet,
})
currentStyles = childComponentStyles
childElement = <ChildComponent />
}
const childProp: ChildProp = {
current: <ChildComponent />,
segment: addSearchParamsIfPageSegment(
childSegmentParam
? childSegmentParam.treeSegment
: childSegment,
query
),
current: childElement,
segment: childPropSegment,
}
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={notFoundComponent}
notFoundStyles={notFoundStyles}
childProp={childProp}
styles={childStyles}
/>,
]
return getParallelRoutePair(childProp, currentStyles)
}
)
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment