Unverified Commit 75a9dfac authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

refactor: error boundary rendering in app-render (#74259)

parent e93f27e3
...@@ -209,12 +209,6 @@ async function createComponentTreeInternal({ ...@@ -209,12 +209,6 @@ async function createComponentTreeInternal({
injectedJS: injectedJSWithCurrentLayout, injectedJS: injectedJSWithCurrentLayout,
}) })
: [] : []
const forbiddenElement = Forbidden ? (
<>
{forbiddenStyles}
<Forbidden />
</>
) : undefined
const [Unauthorized, unauthorizedStyles] = const [Unauthorized, unauthorizedStyles] =
authInterrupts && unauthorized authInterrupts && unauthorized
...@@ -226,6 +220,21 @@ async function createComponentTreeInternal({ ...@@ -226,6 +220,21 @@ async function createComponentTreeInternal({
injectedJS: injectedJSWithCurrentLayout, injectedJS: injectedJSWithCurrentLayout,
}) })
: [] : []
const notFoundElement = NotFound ? (
<>
{notFoundStyles}
<NotFound />
</>
) : undefined
const forbiddenElement = Forbidden ? (
<>
{forbiddenStyles}
<Forbidden />
</>
) : undefined
const unauthorizedElement = Unauthorized ? ( const unauthorizedElement = Unauthorized ? (
<> <>
{unauthorizedStyles} {unauthorizedStyles}
...@@ -411,13 +420,9 @@ async function createComponentTreeInternal({ ...@@ -411,13 +420,9 @@ async function createComponentTreeInternal({
const isChildrenRouteKey = parallelRouteKey === 'children' const isChildrenRouteKey = parallelRouteKey === 'children'
const parallelRoute = parallelRoutes[parallelRouteKey] const parallelRoute = parallelRoutes[parallelRouteKey]
const notFoundComponent = const notFoundComponent = isChildrenRouteKey
NotFound && isChildrenRouteKey ? ( ? notFoundElement
<> : undefined
{notFoundStyles}
<NotFound />
</>
) : undefined
const forbiddenComponent = isChildrenRouteKey const forbiddenComponent = isChildrenRouteKey
? forbiddenElement ? forbiddenElement
...@@ -709,56 +714,30 @@ async function createComponentTreeInternal({ ...@@ -709,56 +714,30 @@ async function createComponentTreeInternal({
// but it's not ideal, as it needlessly invokes the `NotFound` component and renders the `RootLayout` twice. // but it's not ideal, as it needlessly invokes the `NotFound` component and renders the `RootLayout` twice.
// We should instead look into handling the fallback behavior differently in development mode so that it doesn't // We should instead look into handling the fallback behavior differently in development mode so that it doesn't
// rely on the `NotFound` behavior. // rely on the `NotFound` behavior.
if (NotFound) { notfoundClientSegment = createErrorBoundaryClientSegmentRoot({
const notFoundParallelRouteProps = { ErrorBoundaryComponent: NotFound,
children: ( errorElement: notFoundElement,
<> ClientSegmentRoot,
{notFoundStyles} layerAssets,
<NotFound /> SegmentComponent,
</> currentParams,
), })
} forbiddenClientSegment = createErrorBoundaryClientSegmentRoot({
notfoundClientSegment = ( ErrorBoundaryComponent: Forbidden,
<> errorElement: forbiddenElement,
{layerAssets} ClientSegmentRoot,
<ClientSegmentRoot layerAssets,
Component={SegmentComponent} SegmentComponent,
slots={notFoundParallelRouteProps} currentParams,
params={currentParams} })
/> unauthorizedClientSegment = createErrorBoundaryClientSegmentRoot({
</> ErrorBoundaryComponent: Unauthorized,
) errorElement: unauthorizedElement,
} ClientSegmentRoot,
if (Forbidden) { layerAssets,
const forbiddenParallelRouteProps = { SegmentComponent,
children: forbiddenElement, currentParams,
} })
forbiddenClientSegment = (
<>
{layerAssets}
<ClientSegmentRoot
Component={SegmentComponent}
slots={forbiddenParallelRouteProps}
params={currentParams}
/>
</>
)
}
if (Unauthorized) {
const unauthorizedParallelRouteProps = {
children: unauthorizedElement,
}
unauthorizedClientSegment = (
<>
{layerAssets}
<ClientSegmentRoot
Component={SegmentComponent}
slots={unauthorizedParallelRouteProps}
params={currentParams}
/>
</>
)
}
if ( if (
notfoundClientSegment || notfoundClientSegment ||
forbiddenClientSegment || forbiddenClientSegment ||
...@@ -861,3 +840,36 @@ async function MetadataOutlet({ ...@@ -861,3 +840,36 @@ async function MetadataOutlet({
return null return null
} }
MetadataOutlet.displayName = OUTLET_BOUNDARY_NAME MetadataOutlet.displayName = OUTLET_BOUNDARY_NAME
function createErrorBoundaryClientSegmentRoot({
ErrorBoundaryComponent,
errorElement,
ClientSegmentRoot,
layerAssets,
SegmentComponent,
currentParams,
}: {
ErrorBoundaryComponent: React.ComponentType<any> | undefined
errorElement: React.ReactNode
ClientSegmentRoot: React.ComponentType<any>
layerAssets: React.ReactNode
SegmentComponent: React.ComponentType<any>
currentParams: Params
}) {
if (ErrorBoundaryComponent) {
const notFoundParallelRouteProps = {
children: errorElement,
}
return (
<>
{layerAssets}
<ClientSegmentRoot
Component={SegmentComponent}
slots={notFoundParallelRouteProps}
params={currentParams}
/>
</>
)
}
return null
}
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