Commit 5c0efcd7 authored by Tim Neutkens's avatar Tim Neutkens

renderLayoutComponent

parent 25c255ca
......@@ -123,10 +123,6 @@ async function createComponentTreeInternal(
createElement,
Fragment,
SegmentViewNode,
HTTPAccessFallbackBoundary,
ClientSegmentRoot,
createServerParamsForServerSegment,
createPrerenderParamsForClientSegment,
serverHooks: { DynamicServerError },
Postpone,
},
......@@ -561,184 +557,156 @@ async function createComponentTreeInternal(
isPossiblyPartialResponse
)
} else {
const SegmentComponent = Component
const isRootLayoutWithChildrenSlotAndAtLeastOneMoreSlot =
return renderLayoutComponent(
ctx,
Component,
isClientComponent,
rootLayoutAtThisLevel &&
'children' in parallelRoutes &&
Object.keys(parallelRoutes).length > 1
let segmentNode: React.ReactNode
if (isClientComponent) {
let clientSegment: React.ReactNode
if (cacheComponents) {
// Params are omitted when Cache Components is enabled
clientSegment = createElement(ClientSegmentRoot, {
Component: SegmentComponent,
slots: parallelRouteProps,
serverProvidedParams: null,
})
} else if (isStaticGeneration) {
const promiseOfParams =
createPrerenderParamsForClientSegment(currentParams)
clientSegment = createElement(ClientSegmentRoot, {
Component: SegmentComponent,
slots: parallelRouteProps,
serverProvidedParams: {
params: currentParams,
promises: [promiseOfParams],
},
})
} else {
clientSegment = createElement(ClientSegmentRoot, {
Component: SegmentComponent,
slots: parallelRouteProps,
serverProvidedParams: {
params: currentParams,
promises: null,
},
})
}
'children' in parallelRoutes &&
Object.keys(parallelRoutes).length > 1,
currentParams,
parallelRouteProps,
optionalCatchAllParamName,
varyParamsAccumulator,
isRuntimePrefetchable,
NotFound,
Forbidden,
Unauthorized,
notFoundElement,
forbiddenElement,
unauthorizedElement,
notFoundStyles,
layerAssets,
tree,
dir,
isSegmentViewEnabled,
parallelRouteCacheNodeSeedData,
loadingData,
isPossiblyPartialResponse
)
}
}
if (isRootLayoutWithChildrenSlotAndAtLeastOneMoreSlot) {
let notfoundClientSegment: React.ReactNode
let forbiddenClientSegment: React.ReactNode
let unauthorizedClientSegment: React.ReactNode
// TODO-APP: This is a hack to support unmatched parallel routes, which will throw `notFound()`.
// This ensures that a `HTTPAccessFallbackBoundary` is available for when that happens,
// 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
// rely on the `NotFound` behavior.
notfoundClientSegment = createErrorBoundaryClientSegmentRoot({
ctx,
ErrorBoundaryComponent: NotFound,
errorElement: notFoundElement,
ClientSegmentRoot,
layerAssets,
SegmentComponent,
currentParams,
})
forbiddenClientSegment = createErrorBoundaryClientSegmentRoot({
ctx,
ErrorBoundaryComponent: Forbidden,
errorElement: forbiddenElement,
ClientSegmentRoot,
layerAssets,
SegmentComponent,
currentParams,
})
unauthorizedClientSegment = createErrorBoundaryClientSegmentRoot({
ctx,
ErrorBoundaryComponent: Unauthorized,
errorElement: unauthorizedElement,
ClientSegmentRoot,
layerAssets,
SegmentComponent,
currentParams,
})
if (
notfoundClientSegment ||
forbiddenClientSegment ||
unauthorizedClientSegment
) {
segmentNode = createElement(
HTTPAccessFallbackBoundary,
{
key: cacheNodeKey,
notFound: notfoundClientSegment,
forbidden: forbiddenClientSegment,
unauthorized: unauthorizedClientSegment,
},
layerAssets,
clientSegment
)
} else {
segmentNode = createElement(
Fragment,
{
key: cacheNodeKey,
},
layerAssets,
clientSegment
)
}
} else {
segmentNode = createElement(
Fragment,
{
key: cacheNodeKey,
},
layerAssets,
clientSegment
)
}
} else {
const params = createServerParamsForServerSegment(
currentParams,
optionalCatchAllParamName,
varyParamsAccumulator,
isRuntimePrefetchable
)
function renderLayoutComponent(
ctx: AppRenderContext,
SegmentComponent: ComponentType<any>,
isClientComponent: boolean,
isRootLayoutWithChildrenSlotAndAtLeastOneMoreSlot: boolean,
currentParams: Params,
parallelRouteProps: { [key: string]: React.ReactNode },
optionalCatchAllParamName: string | null,
varyParamsAccumulator: VaryParamsAccumulator | null,
isRuntimePrefetchable: boolean,
NotFound: ComponentType<any> | undefined,
Forbidden: ComponentType<any> | undefined,
Unauthorized: ComponentType<any> | undefined,
notFoundElement: React.ReactNode | undefined,
forbiddenElement: React.ReactNode | undefined,
unauthorizedElement: React.ReactNode | undefined,
notFoundStyles: React.ReactNode | undefined,
layerAssets: React.ReactNode,
tree: LoaderTree,
dir: string,
isSegmentViewEnabled: boolean,
parallelRouteCacheNodeSeedData: Record<string, CacheNodeSeedData | null>,
loadingData: LoadingModuleData,
isPossiblyPartialResponse: boolean
): CacheNodeSeedData {
const {
renderOpts: { cacheComponents },
workStore: { isStaticGeneration },
componentMod: {
createElement,
Fragment,
SegmentViewNode,
HTTPAccessFallbackBoundary,
ClientSegmentRoot,
createServerParamsForServerSegment,
createPrerenderParamsForClientSegment,
},
} = ctx
let serverSegment: React.ReactNode
let segmentNode: React.ReactNode
if (isUseCacheFunction(SegmentComponent)) {
const UseCacheLayoutComponent: ComponentType<UseCacheLayoutProps> =
SegmentComponent
if (isClientComponent) {
let clientSegment: React.ReactNode
if (cacheComponents) {
clientSegment = createElement(ClientSegmentRoot, {
Component: SegmentComponent,
slots: parallelRouteProps,
serverProvidedParams: null,
})
} else if (isStaticGeneration) {
const promiseOfParams =
createPrerenderParamsForClientSegment(currentParams)
serverSegment = createElement(
UseCacheLayoutComponent,
{
...parallelRouteProps,
params: params,
$$isLayout: true,
},
// Force static children here so that they're validated.
// See https://github.com/facebook/react/pull/34846
parallelRouteProps.children
)
} else {
serverSegment = createElement(
SegmentComponent,
{
...parallelRouteProps,
params: params,
},
// Force static children here so that they're validated.
// See https://github.com/facebook/react/pull/34846
parallelRouteProps.children
)
}
clientSegment = createElement(ClientSegmentRoot, {
Component: SegmentComponent,
slots: parallelRouteProps,
serverProvidedParams: {
params: currentParams,
promises: [promiseOfParams],
},
})
} else {
clientSegment = createElement(ClientSegmentRoot, {
Component: SegmentComponent,
slots: parallelRouteProps,
serverProvidedParams: {
params: currentParams,
promises: null,
},
})
}
if (isRootLayoutWithChildrenSlotAndAtLeastOneMoreSlot) {
// TODO-APP: This is a hack to support unmatched parallel routes, which will throw `notFound()`.
// This ensures that a `HTTPAccessFallbackBoundary` is available for when that happens,
// 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
// rely on the `NotFound` behavior.
if (isRootLayoutWithChildrenSlotAndAtLeastOneMoreSlot) {
// TODO-APP: This is a hack to support unmatched parallel routes, which will throw `notFound()`.
// This ensures that a `HTTPAccessFallbackBoundary` is available for when that happens,
// 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
// rely on the `NotFound` behavior.
const notfoundClientSegment = createErrorBoundaryClientSegmentRoot({
ctx,
ErrorBoundaryComponent: NotFound,
errorElement: notFoundElement,
ClientSegmentRoot,
layerAssets,
SegmentComponent,
currentParams,
})
const forbiddenClientSegment = createErrorBoundaryClientSegmentRoot({
ctx,
ErrorBoundaryComponent: Forbidden,
errorElement: forbiddenElement,
ClientSegmentRoot,
layerAssets,
SegmentComponent,
currentParams,
})
const unauthorizedClientSegment = createErrorBoundaryClientSegmentRoot({
ctx,
ErrorBoundaryComponent: Unauthorized,
errorElement: unauthorizedElement,
ClientSegmentRoot,
layerAssets,
SegmentComponent,
currentParams,
})
if (
notfoundClientSegment ||
forbiddenClientSegment ||
unauthorizedClientSegment
) {
segmentNode = createElement(
HTTPAccessFallbackBoundary,
{
key: cacheNodeKey,
notFound: notFoundElement
? createElement(
Fragment,
null,
layerAssets,
createElement(
SegmentComponent,
{
params: params,
},
notFoundStyles,
notFoundElement
)
)
: undefined,
notFound: notfoundClientSegment,
forbidden: forbiddenClientSegment,
unauthorized: unauthorizedClientSegment,
},
layerAssets,
serverSegment
clientSegment
)
} else {
segmentNode = createElement(
......@@ -747,37 +715,122 @@ async function createComponentTreeInternal(
key: cacheNodeKey,
},
layerAssets,
serverSegment
clientSegment
)
}
} else {
segmentNode = createElement(
Fragment,
{
key: cacheNodeKey,
},
layerAssets,
clientSegment
)
}
} else {
const params = createServerParamsForServerSegment(
currentParams,
optionalCatchAllParamName,
varyParamsAccumulator,
isRuntimePrefetchable
)
const layoutFilePath = getConventionPathByType(tree, dir, 'layout')
const wrappedSegmentNode =
isSegmentViewEnabled && layoutFilePath
? createElement(
SegmentViewNode,
{
key: 'layout',
type: 'layout',
pagePath: layoutFilePath,
},
segmentNode
)
: segmentNode
// For layouts we just render the component
return createSeedData(
ctx,
wrappedSegmentNode,
parallelRouteCacheNodeSeedData,
loadingData,
isPossiblyPartialResponse,
isRuntimePrefetchable,
let serverSegment: React.ReactNode
varyParamsAccumulator
)
if (isUseCacheFunction(SegmentComponent)) {
const UseCacheLayoutComponent: ComponentType<UseCacheLayoutProps> =
SegmentComponent
serverSegment = createElement(
UseCacheLayoutComponent,
{
...parallelRouteProps,
params: params,
$$isLayout: true,
},
// Force static children here so that they're validated.
// See https://github.com/facebook/react/pull/34846
parallelRouteProps.children
)
} else {
serverSegment = createElement(
SegmentComponent,
{
...parallelRouteProps,
params: params,
},
// Force static children here so that they're validated.
// See https://github.com/facebook/react/pull/34846
parallelRouteProps.children
)
}
if (isRootLayoutWithChildrenSlotAndAtLeastOneMoreSlot) {
// TODO-APP: This is a hack to support unmatched parallel routes, which will throw `notFound()`.
// This ensures that a `HTTPAccessFallbackBoundary` is available for when that happens,
// 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
// rely on the `NotFound` behavior.
segmentNode = createElement(
HTTPAccessFallbackBoundary,
{
key: cacheNodeKey,
notFound: notFoundElement
? createElement(
Fragment,
null,
layerAssets,
createElement(
SegmentComponent,
{
params: params,
},
notFoundStyles,
notFoundElement
)
)
: undefined,
},
layerAssets,
serverSegment
)
} else {
segmentNode = createElement(
Fragment,
{
key: cacheNodeKey,
},
layerAssets,
serverSegment
)
}
}
const layoutFilePath = getConventionPathByType(tree, dir, 'layout')
const wrappedSegmentNode =
isSegmentViewEnabled && layoutFilePath
? createElement(
SegmentViewNode,
{
key: 'layout',
type: 'layout',
pagePath: layoutFilePath,
},
segmentNode
)
: segmentNode
return createSeedData(
ctx,
wrappedSegmentNode,
parallelRouteCacheNodeSeedData,
loadingData,
isPossiblyPartialResponse,
isRuntimePrefetchable,
varyParamsAccumulator
)
}
function renderPageComponent(
......
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