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( ...@@ -825,24 +825,10 @@ export async function renderToHTMLOrFlight(
const notFoundComponent = const notFoundComponent =
NotFound && isChildrenRouteKey ? <NotFound /> : undefined NotFound && isChildrenRouteKey ? <NotFound /> : undefined
// if we're prefetching and that there's a Loading component, we bail out function getParallelRoutePair(
// otherwise we keep rendering for the prefetch. currentChildProp: ChildProp,
// We also want to bail out if there's no Loading component in the tree. currentStyles: React.ReactNode
if ( ): [string, React.ReactNode] {
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. // This is turned back into an object below.
return [ return [
parallelRouteKey, parallelRouteKey,
...@@ -851,6 +837,7 @@ export async function renderToHTMLOrFlight( ...@@ -851,6 +837,7 @@ export async function renderToHTMLOrFlight(
segmentPath={createSegmentPath(currentSegmentPath)} segmentPath={createSegmentPath(currentSegmentPath)}
loading={Loading ? <Loading /> : undefined} loading={Loading ? <Loading /> : undefined}
loadingStyles={loadingStyles} 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)} hasLoading={Boolean(Loading)}
error={ErrorComponent} error={ErrorComponent}
errorStyles={errorStyles} errorStyles={errorStyles}
...@@ -862,14 +849,32 @@ export async function renderToHTMLOrFlight( ...@@ -862,14 +849,32 @@ export async function renderToHTMLOrFlight(
templateStyles={templateStyles} templateStyles={templateStyles}
notFound={notFoundComponent} notFound={notFoundComponent}
notFoundStyles={notFoundStyles} notFoundStyles={notFoundStyles}
childProp={childProp} childProp={currentChildProp}
styles={currentStyles}
/>, />,
] ]
} }
// Create the child component // if we're prefetching and that there's a Loading component, we bail out
const { Component: ChildComponent, styles: childStyles } = // otherwise we keep rendering for the prefetch.
await createComponentTree({ // 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) => { createSegmentPath: (child) => {
return createSegmentPath([...currentSegmentPath, ...child]) return createSegmentPath([...currentSegmentPath, ...child])
}, },
...@@ -883,42 +888,16 @@ export async function renderToHTMLOrFlight( ...@@ -883,42 +888,16 @@ export async function renderToHTMLOrFlight(
metadataOutlet, metadataOutlet,
}) })
currentStyles = childComponentStyles
childElement = <ChildComponent />
}
const childProp: ChildProp = { const childProp: ChildProp = {
current: <ChildComponent />, current: childElement,
segment: addSearchParamsIfPageSegment( segment: childPropSegment,
childSegmentParam
? childSegmentParam.treeSegment
: childSegment,
query
),
} }
const segmentPath = createSegmentPath(currentSegmentPath) return getParallelRoutePair(childProp, currentStyles)
// 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}
/>,
]
} }
) )
) )
......
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