|
|
import type { CacheNodeSeedData, PreloadCallbacks } from './types' |
|
|
import React from 'react' |
|
|
import { |
|
|
isClientReference, |
|
|
isUseCacheFunction, |
|
|
} from '../../lib/client-and-server-references' |
|
|
import { getLayoutOrPageModule } from '../lib/app-dir-module' |
|
|
import type { LoaderTree } from '../lib/app-dir-module' |
|
|
import { interopDefault } from './interop-default' |
|
|
import { parseLoaderTree } from './parse-loader-tree' |
|
|
import type { AppRenderContext, GetDynamicParamFromSegment } from './app-render' |
|
|
import { createComponentStylesAndScripts } from './create-component-styles-and-scripts' |
|
|
import { getLayerAssets } from './get-layer-assets' |
|
|
import { hasLoadingComponentInTree } from './has-loading-component-in-tree' |
|
|
import { validateRevalidate } from '../lib/patch-fetch' |
|
|
import { PARALLEL_ROUTE_DEFAULT_PATH } from '../../client/components/builtin/default' |
|
|
import { getTracer } from '../lib/trace/tracer' |
|
|
import { NextNodeServerSpan } from '../lib/trace/constants' |
|
|
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout' |
|
|
import type { LoadingModuleData } from '../../shared/lib/app-router-context.shared-runtime' |
|
|
import type { Params } from '../request/params' |
|
|
import { workUnitAsyncStorage } from './work-unit-async-storage.external' |
|
|
import { OUTLET_BOUNDARY_NAME } from '../../lib/metadata/metadata-constants' |
|
|
import type { |
|
|
UseCacheLayoutComponentProps, |
|
|
UseCachePageComponentProps, |
|
|
} from '../use-cache/use-cache-wrapper' |
|
|
import { DEFAULT_SEGMENT_KEY } from '../../shared/lib/segment' |
|
|
import { |
|
|
BOUNDARY_PREFIX, |
|
|
BOUNDARY_SUFFIX, |
|
|
BUILTIN_PREFIX, |
|
|
getConventionPathByType, |
|
|
isNextjsBuiltinFilePath, |
|
|
} from './segment-explorer-path' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function createComponentTree(props: { |
|
|
loaderTree: LoaderTree |
|
|
parentParams: Params |
|
|
rootLayoutIncluded: boolean |
|
|
injectedCSS: Set<string> |
|
|
injectedJS: Set<string> |
|
|
injectedFontPreloadTags: Set<string> |
|
|
getMetadataReady: () => Promise<void> |
|
|
getViewportReady: () => Promise<void> |
|
|
ctx: AppRenderContext |
|
|
missingSlots?: Set<string> |
|
|
preloadCallbacks: PreloadCallbacks |
|
|
authInterrupts: boolean |
|
|
StreamingMetadataOutlet: React.ComponentType | null |
|
|
}): Promise<CacheNodeSeedData> { |
|
|
return getTracer().trace( |
|
|
NextNodeServerSpan.createComponentTree, |
|
|
{ |
|
|
spanName: 'build component tree', |
|
|
}, |
|
|
() => createComponentTreeInternal(props, true) |
|
|
) |
|
|
} |
|
|
|
|
|
function errorMissingDefaultExport( |
|
|
pagePath: string, |
|
|
convention: string |
|
|
): never { |
|
|
const normalizedPagePath = pagePath === '/' ? '' : pagePath |
|
|
throw new Error( |
|
|
`The default export is not a React Component in "${normalizedPagePath}/${convention}"` |
|
|
) |
|
|
} |
|
|
|
|
|
const cacheNodeKey = 'c' |
|
|
|
|
|
async function createComponentTreeInternal( |
|
|
{ |
|
|
loaderTree: tree, |
|
|
parentParams, |
|
|
rootLayoutIncluded, |
|
|
injectedCSS, |
|
|
injectedJS, |
|
|
injectedFontPreloadTags, |
|
|
getViewportReady, |
|
|
getMetadataReady, |
|
|
ctx, |
|
|
missingSlots, |
|
|
preloadCallbacks, |
|
|
authInterrupts, |
|
|
StreamingMetadataOutlet, |
|
|
}: { |
|
|
loaderTree: LoaderTree |
|
|
parentParams: Params |
|
|
rootLayoutIncluded: boolean |
|
|
injectedCSS: Set<string> |
|
|
injectedJS: Set<string> |
|
|
injectedFontPreloadTags: Set<string> |
|
|
getViewportReady: () => Promise<void> |
|
|
getMetadataReady: () => Promise<void> |
|
|
ctx: AppRenderContext |
|
|
missingSlots?: Set<string> |
|
|
preloadCallbacks: PreloadCallbacks |
|
|
authInterrupts: boolean |
|
|
StreamingMetadataOutlet: React.ComponentType | null |
|
|
}, |
|
|
isRoot: boolean |
|
|
): Promise<CacheNodeSeedData> { |
|
|
const { |
|
|
renderOpts: { nextConfigOutput, experimental }, |
|
|
workStore, |
|
|
componentMod: { |
|
|
SegmentViewNode, |
|
|
HTTPAccessFallbackBoundary, |
|
|
LayoutRouter, |
|
|
RenderFromTemplateContext, |
|
|
OutletBoundary, |
|
|
ClientPageRoot, |
|
|
ClientSegmentRoot, |
|
|
createServerSearchParamsForServerPage, |
|
|
createPrerenderSearchParamsForClientPage, |
|
|
createServerParamsForServerSegment, |
|
|
createPrerenderParamsForClientSegment, |
|
|
serverHooks: { DynamicServerError }, |
|
|
Postpone, |
|
|
}, |
|
|
pagePath, |
|
|
getDynamicParamFromSegment, |
|
|
isPrefetch, |
|
|
query, |
|
|
} = ctx |
|
|
|
|
|
const { page, conventionPath, segment, modules, parallelRoutes } = |
|
|
parseLoaderTree(tree) |
|
|
|
|
|
const { |
|
|
layout, |
|
|
template, |
|
|
error, |
|
|
loading, |
|
|
'not-found': notFound, |
|
|
forbidden, |
|
|
unauthorized, |
|
|
} = modules |
|
|
|
|
|
const injectedCSSWithCurrentLayout = new Set(injectedCSS) |
|
|
const injectedJSWithCurrentLayout = new Set(injectedJS) |
|
|
const injectedFontPreloadTagsWithCurrentLayout = new Set( |
|
|
injectedFontPreloadTags |
|
|
) |
|
|
|
|
|
const layerAssets = getLayerAssets({ |
|
|
preloadCallbacks, |
|
|
ctx, |
|
|
layoutOrPagePath: conventionPath, |
|
|
injectedCSS: injectedCSSWithCurrentLayout, |
|
|
injectedJS: injectedJSWithCurrentLayout, |
|
|
injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout, |
|
|
}) |
|
|
|
|
|
const [Template, templateStyles, templateScripts] = template |
|
|
? await createComponentStylesAndScripts({ |
|
|
ctx, |
|
|
filePath: template[1], |
|
|
getComponent: template[0], |
|
|
injectedCSS: injectedCSSWithCurrentLayout, |
|
|
injectedJS: injectedJSWithCurrentLayout, |
|
|
}) |
|
|
: [React.Fragment] |
|
|
|
|
|
const [ErrorComponent, errorStyles, errorScripts] = error |
|
|
? await createComponentStylesAndScripts({ |
|
|
ctx, |
|
|
filePath: error[1], |
|
|
getComponent: error[0], |
|
|
injectedCSS: injectedCSSWithCurrentLayout, |
|
|
injectedJS: injectedJSWithCurrentLayout, |
|
|
}) |
|
|
: [] |
|
|
|
|
|
const [Loading, loadingStyles, loadingScripts] = loading |
|
|
? await createComponentStylesAndScripts({ |
|
|
ctx, |
|
|
filePath: loading[1], |
|
|
getComponent: loading[0], |
|
|
injectedCSS: injectedCSSWithCurrentLayout, |
|
|
injectedJS: injectedJSWithCurrentLayout, |
|
|
}) |
|
|
: [] |
|
|
|
|
|
const isLayout = typeof layout !== 'undefined' |
|
|
const isPage = typeof page !== 'undefined' |
|
|
const { mod: layoutOrPageMod, modType } = await getTracer().trace( |
|
|
NextNodeServerSpan.getLayoutOrPageModule, |
|
|
{ |
|
|
hideSpan: !(isLayout || isPage), |
|
|
spanName: 'resolve segment modules', |
|
|
attributes: { |
|
|
'next.segment': segment, |
|
|
}, |
|
|
}, |
|
|
() => getLayoutOrPageModule(tree) |
|
|
) |
|
|
|
|
|
const gracefullyDegrade = !!ctx.renderOpts.botType |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const rootLayoutAtThisLevel = isLayout && !rootLayoutIncluded |
|
|
|
|
|
|
|
|
|
|
|
const rootLayoutIncludedAtThisLevelOrAbove = |
|
|
rootLayoutIncluded || rootLayoutAtThisLevel |
|
|
|
|
|
const [NotFound, notFoundStyles] = notFound |
|
|
? await createComponentStylesAndScripts({ |
|
|
ctx, |
|
|
filePath: notFound[1], |
|
|
getComponent: notFound[0], |
|
|
injectedCSS: injectedCSSWithCurrentLayout, |
|
|
injectedJS: injectedJSWithCurrentLayout, |
|
|
}) |
|
|
: [] |
|
|
|
|
|
const [Forbidden, forbiddenStyles] = |
|
|
authInterrupts && forbidden |
|
|
? await createComponentStylesAndScripts({ |
|
|
ctx, |
|
|
filePath: forbidden[1], |
|
|
getComponent: forbidden[0], |
|
|
injectedCSS: injectedCSSWithCurrentLayout, |
|
|
injectedJS: injectedJSWithCurrentLayout, |
|
|
}) |
|
|
: [] |
|
|
|
|
|
const [Unauthorized, unauthorizedStyles] = |
|
|
authInterrupts && unauthorized |
|
|
? await createComponentStylesAndScripts({ |
|
|
ctx, |
|
|
filePath: unauthorized[1], |
|
|
getComponent: unauthorized[0], |
|
|
injectedCSS: injectedCSSWithCurrentLayout, |
|
|
injectedJS: injectedJSWithCurrentLayout, |
|
|
}) |
|
|
: [] |
|
|
|
|
|
let dynamic = layoutOrPageMod?.dynamic |
|
|
|
|
|
if (nextConfigOutput === 'export') { |
|
|
if (!dynamic || dynamic === 'auto') { |
|
|
dynamic = 'error' |
|
|
} else if (dynamic === 'force-dynamic') { |
|
|
|
|
|
throw new StaticGenBailoutError( |
|
|
`Page with \`dynamic = "force-dynamic"\` couldn't be exported. \`output: "export"\` requires all pages be renderable statically because there is no runtime server to dynamically render routes in this output format. Learn more: https://nextjs.org/docs/app/building-your-application/deploying/static-exports` |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
if (typeof dynamic === 'string') { |
|
|
|
|
|
|
|
|
|
|
|
if (dynamic === 'error') { |
|
|
workStore.dynamicShouldError = true |
|
|
} else if (dynamic === 'force-dynamic') { |
|
|
workStore.forceDynamic = true |
|
|
|
|
|
|
|
|
if (workStore.isStaticGeneration && !experimental.isRoutePPREnabled) { |
|
|
|
|
|
|
|
|
const err = new DynamicServerError( |
|
|
`Page with \`dynamic = "force-dynamic"\` won't be rendered statically.` |
|
|
) |
|
|
workStore.dynamicUsageDescription = err.message |
|
|
workStore.dynamicUsageStack = err.stack |
|
|
throw err |
|
|
} |
|
|
} else { |
|
|
workStore.dynamicShouldError = false |
|
|
workStore.forceStatic = dynamic === 'force-static' |
|
|
} |
|
|
} |
|
|
|
|
|
if (typeof layoutOrPageMod?.fetchCache === 'string') { |
|
|
workStore.fetchCache = layoutOrPageMod?.fetchCache |
|
|
} |
|
|
|
|
|
if (typeof layoutOrPageMod?.revalidate !== 'undefined') { |
|
|
validateRevalidate(layoutOrPageMod?.revalidate, workStore.route) |
|
|
} |
|
|
|
|
|
if (typeof layoutOrPageMod?.revalidate === 'number') { |
|
|
const defaultRevalidate = layoutOrPageMod.revalidate as number |
|
|
|
|
|
const workUnitStore = workUnitAsyncStorage.getStore() |
|
|
|
|
|
if (workUnitStore) { |
|
|
switch (workUnitStore.type) { |
|
|
case 'prerender': |
|
|
case 'prerender-legacy': |
|
|
case 'prerender-ppr': |
|
|
if (workUnitStore.revalidate > defaultRevalidate) { |
|
|
workUnitStore.revalidate = defaultRevalidate |
|
|
} |
|
|
break |
|
|
case 'request': |
|
|
|
|
|
break |
|
|
|
|
|
case 'cache': |
|
|
case 'private-cache': |
|
|
case 'prerender-client': |
|
|
case 'unstable-cache': |
|
|
break |
|
|
default: |
|
|
workUnitStore satisfies never |
|
|
} |
|
|
} |
|
|
|
|
|
if ( |
|
|
!workStore.forceStatic && |
|
|
workStore.isStaticGeneration && |
|
|
defaultRevalidate === 0 && |
|
|
|
|
|
|
|
|
!experimental.isRoutePPREnabled |
|
|
) { |
|
|
const dynamicUsageDescription = `revalidate: 0 configured ${segment}` |
|
|
workStore.dynamicUsageDescription = dynamicUsageDescription |
|
|
|
|
|
throw new DynamicServerError(dynamicUsageDescription) |
|
|
} |
|
|
} |
|
|
|
|
|
const isStaticGeneration = workStore.isStaticGeneration |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const isPossiblyPartialResponse = |
|
|
isStaticGeneration && experimental.isRoutePPREnabled === true |
|
|
|
|
|
const LayoutOrPage: React.ComponentType<any> | undefined = layoutOrPageMod |
|
|
? interopDefault(layoutOrPageMod) |
|
|
: undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let MaybeComponent = LayoutOrPage |
|
|
|
|
|
if (process.env.NODE_ENV === 'development') { |
|
|
const { isValidElementType } = |
|
|
require('next/dist/compiled/react-is') as typeof import('next/dist/compiled/react-is') |
|
|
if ( |
|
|
typeof MaybeComponent !== 'undefined' && |
|
|
!isValidElementType(MaybeComponent) |
|
|
) { |
|
|
errorMissingDefaultExport(pagePath, modType ?? 'page') |
|
|
} |
|
|
|
|
|
if ( |
|
|
typeof ErrorComponent !== 'undefined' && |
|
|
!isValidElementType(ErrorComponent) |
|
|
) { |
|
|
errorMissingDefaultExport(pagePath, 'error') |
|
|
} |
|
|
|
|
|
if (typeof Loading !== 'undefined' && !isValidElementType(Loading)) { |
|
|
errorMissingDefaultExport(pagePath, 'loading') |
|
|
} |
|
|
|
|
|
if (typeof NotFound !== 'undefined' && !isValidElementType(NotFound)) { |
|
|
errorMissingDefaultExport(pagePath, 'not-found') |
|
|
} |
|
|
|
|
|
if (typeof Forbidden !== 'undefined' && !isValidElementType(Forbidden)) { |
|
|
errorMissingDefaultExport(pagePath, 'forbidden') |
|
|
} |
|
|
|
|
|
if ( |
|
|
typeof Unauthorized !== 'undefined' && |
|
|
!isValidElementType(Unauthorized) |
|
|
) { |
|
|
errorMissingDefaultExport(pagePath, 'unauthorized') |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const segmentParam = getDynamicParamFromSegment(segment) |
|
|
|
|
|
|
|
|
let currentParams: Params = parentParams |
|
|
if (segmentParam && segmentParam.value !== null) { |
|
|
currentParams = { |
|
|
...parentParams, |
|
|
[segmentParam.param]: segmentParam.value, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const actualSegment = segmentParam ? segmentParam.treeSegment : segment |
|
|
const isSegmentViewEnabled = |
|
|
process.env.NODE_ENV === 'development' && |
|
|
ctx.renderOpts.devtoolSegmentExplorer |
|
|
const dir = |
|
|
process.env.NEXT_RUNTIME === 'edge' |
|
|
? process.env.__NEXT_EDGE_PROJECT_DIR! |
|
|
: ctx.renderOpts.dir || '' |
|
|
|
|
|
|
|
|
const metadataOutlet = StreamingMetadataOutlet ? ( |
|
|
<StreamingMetadataOutlet /> |
|
|
) : ( |
|
|
<MetadataOutlet ready={getMetadataReady} /> |
|
|
) |
|
|
|
|
|
const [notFoundElement, notFoundFilePath] = |
|
|
await createBoundaryConventionElement({ |
|
|
ctx, |
|
|
conventionName: 'not-found', |
|
|
Component: NotFound, |
|
|
styles: notFoundStyles, |
|
|
tree, |
|
|
}) |
|
|
|
|
|
const [forbiddenElement] = await createBoundaryConventionElement({ |
|
|
ctx, |
|
|
conventionName: 'forbidden', |
|
|
Component: Forbidden, |
|
|
styles: forbiddenStyles, |
|
|
tree, |
|
|
}) |
|
|
|
|
|
const [unauthorizedElement] = await createBoundaryConventionElement({ |
|
|
ctx, |
|
|
conventionName: 'unauthorized', |
|
|
Component: Unauthorized, |
|
|
styles: unauthorizedStyles, |
|
|
tree, |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
const parallelRouteMap = await Promise.all( |
|
|
Object.keys(parallelRoutes).map( |
|
|
async ( |
|
|
parallelRouteKey |
|
|
): Promise<[string, React.ReactNode, CacheNodeSeedData | null]> => { |
|
|
const isChildrenRouteKey = parallelRouteKey === 'children' |
|
|
const parallelRoute = parallelRoutes[parallelRouteKey] |
|
|
|
|
|
const notFoundComponent = isChildrenRouteKey |
|
|
? notFoundElement |
|
|
: undefined |
|
|
|
|
|
const forbiddenComponent = isChildrenRouteKey |
|
|
? forbiddenElement |
|
|
: undefined |
|
|
|
|
|
const unauthorizedComponent = isChildrenRouteKey |
|
|
? unauthorizedElement |
|
|
: undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let childCacheNodeSeedData: CacheNodeSeedData | null = null |
|
|
|
|
|
if ( |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isPrefetch && |
|
|
(Loading || !hasLoadingComponentInTree(parallelRoute)) && |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!experimental.isRoutePPREnabled |
|
|
) { |
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'development' && missingSlots) { |
|
|
|
|
|
|
|
|
const parsedTree = parseLoaderTree(parallelRoute) |
|
|
if ( |
|
|
parsedTree.conventionPath?.endsWith(PARALLEL_ROUTE_DEFAULT_PATH) |
|
|
) { |
|
|
missingSlots.add(parallelRouteKey) |
|
|
} |
|
|
} |
|
|
|
|
|
const seedData = await createComponentTreeInternal( |
|
|
{ |
|
|
loaderTree: parallelRoute, |
|
|
parentParams: currentParams, |
|
|
rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove, |
|
|
injectedCSS: injectedCSSWithCurrentLayout, |
|
|
injectedJS: injectedJSWithCurrentLayout, |
|
|
injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout, |
|
|
|
|
|
|
|
|
getMetadataReady: isChildrenRouteKey |
|
|
? getMetadataReady |
|
|
: () => Promise.resolve(), |
|
|
getViewportReady: isChildrenRouteKey |
|
|
? getViewportReady |
|
|
: () => Promise.resolve(), |
|
|
ctx, |
|
|
missingSlots, |
|
|
preloadCallbacks, |
|
|
authInterrupts, |
|
|
|
|
|
|
|
|
StreamingMetadataOutlet: isChildrenRouteKey |
|
|
? StreamingMetadataOutlet |
|
|
: null, |
|
|
}, |
|
|
false |
|
|
) |
|
|
|
|
|
childCacheNodeSeedData = seedData |
|
|
} |
|
|
|
|
|
const templateNode = ( |
|
|
<Template> |
|
|
<RenderFromTemplateContext /> |
|
|
</Template> |
|
|
) |
|
|
|
|
|
const templateFilePath = getConventionPathByType(tree, dir, 'template') |
|
|
const errorFilePath = getConventionPathByType(tree, dir, 'error') |
|
|
const loadingFilePath = getConventionPathByType(tree, dir, 'loading') |
|
|
const globalErrorFilePath = isRoot |
|
|
? getConventionPathByType(tree, dir, 'global-error') |
|
|
: undefined |
|
|
|
|
|
const wrappedErrorStyles = |
|
|
isSegmentViewEnabled && errorFilePath ? ( |
|
|
<SegmentViewNode type="error" pagePath={errorFilePath}> |
|
|
{errorStyles} |
|
|
</SegmentViewNode> |
|
|
) : ( |
|
|
errorStyles |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const fileNameSuffix = BOUNDARY_SUFFIX |
|
|
const segmentViewBoundaries = isSegmentViewEnabled ? ( |
|
|
<> |
|
|
{notFoundFilePath && ( |
|
|
<SegmentViewNode |
|
|
type={`${BOUNDARY_PREFIX}not-found`} |
|
|
pagePath={notFoundFilePath + fileNameSuffix} |
|
|
/> |
|
|
)} |
|
|
{loadingFilePath && ( |
|
|
<SegmentViewNode |
|
|
type={`${BOUNDARY_PREFIX}loading`} |
|
|
pagePath={loadingFilePath + fileNameSuffix} |
|
|
/> |
|
|
)} |
|
|
{errorFilePath && ( |
|
|
<SegmentViewNode |
|
|
type={`${BOUNDARY_PREFIX}error`} |
|
|
pagePath={errorFilePath + fileNameSuffix} |
|
|
/> |
|
|
)} |
|
|
{/* Only show global-error when it's the builtin one */} |
|
|
{globalErrorFilePath && ( |
|
|
<SegmentViewNode |
|
|
type={`${BOUNDARY_PREFIX}global-error`} |
|
|
pagePath={ |
|
|
isNextjsBuiltinFilePath(globalErrorFilePath) |
|
|
? `${BUILTIN_PREFIX}global-error.js${fileNameSuffix}` |
|
|
: globalErrorFilePath |
|
|
} |
|
|
/> |
|
|
)} |
|
|
{/* do not surface forbidden and unauthorized boundaries yet as they're unstable */} |
|
|
</> |
|
|
) : null |
|
|
|
|
|
return [ |
|
|
parallelRouteKey, |
|
|
<LayoutRouter |
|
|
parallelRouterKey={parallelRouteKey} |
|
|
// 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. |
|
|
error={ErrorComponent} |
|
|
errorStyles={wrappedErrorStyles} |
|
|
errorScripts={errorScripts} |
|
|
template={ |
|
|
// Only render SegmentViewNode when there's an actual template |
|
|
isSegmentViewEnabled && templateFilePath ? ( |
|
|
<SegmentViewNode type="template" pagePath={templateFilePath}> |
|
|
{templateNode} |
|
|
</SegmentViewNode> |
|
|
) : ( |
|
|
templateNode |
|
|
) |
|
|
} |
|
|
templateStyles={templateStyles} |
|
|
templateScripts={templateScripts} |
|
|
notFound={notFoundComponent} |
|
|
forbidden={forbiddenComponent} |
|
|
unauthorized={unauthorizedComponent} |
|
|
{...(isSegmentViewEnabled && { segmentViewBoundaries })} |
|
|
// Since gracefullyDegrade only applies to bots, only |
|
|
// pass it when we're in a bot context to avoid extra bytes. |
|
|
{...(gracefullyDegrade && { gracefullyDegrade })} |
|
|
/>, |
|
|
childCacheNodeSeedData, |
|
|
] |
|
|
} |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
let parallelRouteProps: { [key: string]: React.ReactNode } = {} |
|
|
let parallelRouteCacheNodeSeedData: { |
|
|
[key: string]: CacheNodeSeedData | null |
|
|
} = {} |
|
|
for (const parallelRoute of parallelRouteMap) { |
|
|
const [parallelRouteKey, parallelRouteProp, flightData] = parallelRoute |
|
|
parallelRouteProps[parallelRouteKey] = parallelRouteProp |
|
|
parallelRouteCacheNodeSeedData[parallelRouteKey] = flightData |
|
|
} |
|
|
|
|
|
let loadingElement = Loading ? <Loading key="l" /> : null |
|
|
const loadingFilePath = getConventionPathByType(tree, dir, 'loading') |
|
|
if (isSegmentViewEnabled && loadingElement) { |
|
|
if (loadingFilePath) { |
|
|
loadingElement = ( |
|
|
<SegmentViewNode |
|
|
key={cacheNodeKey + '-loading'} |
|
|
type="loading" |
|
|
pagePath={loadingFilePath} |
|
|
> |
|
|
{loadingElement} |
|
|
</SegmentViewNode> |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
const loadingData: LoadingModuleData = loadingElement |
|
|
? [loadingElement, loadingStyles, loadingScripts] |
|
|
: null |
|
|
|
|
|
|
|
|
if (!MaybeComponent) { |
|
|
return [ |
|
|
actualSegment, |
|
|
<React.Fragment key={cacheNodeKey}> |
|
|
{layerAssets} |
|
|
{parallelRouteProps.children} |
|
|
</React.Fragment>, |
|
|
parallelRouteCacheNodeSeedData, |
|
|
loadingData, |
|
|
isPossiblyPartialResponse, |
|
|
] |
|
|
} |
|
|
|
|
|
const Component = MaybeComponent |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
workStore.isStaticGeneration && |
|
|
workStore.forceDynamic && |
|
|
experimental.isRoutePPREnabled |
|
|
) { |
|
|
return [ |
|
|
actualSegment, |
|
|
<React.Fragment key={cacheNodeKey}> |
|
|
<Postpone |
|
|
reason='dynamic = "force-dynamic" was used' |
|
|
route={workStore.route} |
|
|
/> |
|
|
{layerAssets} |
|
|
</React.Fragment>, |
|
|
parallelRouteCacheNodeSeedData, |
|
|
loadingData, |
|
|
true, |
|
|
] |
|
|
} |
|
|
|
|
|
const isClientComponent = isClientReference(layoutOrPageMod) |
|
|
|
|
|
if ( |
|
|
process.env.NODE_ENV === 'development' && |
|
|
'params' in parallelRouteProps |
|
|
) { |
|
|
|
|
|
console.error( |
|
|
`"params" is a reserved prop in Layouts and Pages and cannot be used as the name of a parallel route in ${segment}` |
|
|
) |
|
|
} |
|
|
|
|
|
if (isPage) { |
|
|
const PageComponent = Component |
|
|
|
|
|
|
|
|
let pageElement: React.ReactNode |
|
|
if (isClientComponent) { |
|
|
if (isStaticGeneration) { |
|
|
const promiseOfParams = |
|
|
createPrerenderParamsForClientSegment(currentParams) |
|
|
const promiseOfSearchParams = |
|
|
createPrerenderSearchParamsForClientPage(workStore) |
|
|
pageElement = ( |
|
|
<ClientPageRoot |
|
|
Component={PageComponent} |
|
|
searchParams={query} |
|
|
params={currentParams} |
|
|
promises={[promiseOfSearchParams, promiseOfParams]} |
|
|
/> |
|
|
) |
|
|
} else { |
|
|
pageElement = ( |
|
|
<ClientPageRoot |
|
|
Component={PageComponent} |
|
|
searchParams={query} |
|
|
params={currentParams} |
|
|
/> |
|
|
) |
|
|
} |
|
|
} else { |
|
|
|
|
|
|
|
|
const params = createServerParamsForServerSegment( |
|
|
currentParams, |
|
|
workStore |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let searchParams = createServerSearchParamsForServerPage(query, workStore) |
|
|
|
|
|
if (isUseCacheFunction(PageComponent)) { |
|
|
const UseCachePageComponent: React.ComponentType<UseCachePageComponentProps> = |
|
|
PageComponent |
|
|
|
|
|
pageElement = ( |
|
|
<UseCachePageComponent |
|
|
params={params} |
|
|
searchParams={searchParams} |
|
|
$$isPageComponent |
|
|
/> |
|
|
) |
|
|
} else { |
|
|
pageElement = ( |
|
|
<PageComponent params={params} searchParams={searchParams} /> |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
const isDefaultSegment = segment === DEFAULT_SEGMENT_KEY |
|
|
const pageFilePath = |
|
|
getConventionPathByType(tree, dir, 'page') ?? |
|
|
getConventionPathByType(tree, dir, 'defaultPage') |
|
|
const segmentType = isDefaultSegment ? 'default' : 'page' |
|
|
const wrappedPageElement = |
|
|
isSegmentViewEnabled && pageFilePath ? ( |
|
|
<SegmentViewNode |
|
|
key={cacheNodeKey + '-' + segmentType} |
|
|
type={segmentType} |
|
|
pagePath={pageFilePath} |
|
|
> |
|
|
{pageElement} |
|
|
</SegmentViewNode> |
|
|
) : ( |
|
|
pageElement |
|
|
) |
|
|
|
|
|
return [ |
|
|
actualSegment, |
|
|
<React.Fragment key={cacheNodeKey}> |
|
|
{wrappedPageElement} |
|
|
{layerAssets} |
|
|
<OutletBoundary> |
|
|
<MetadataOutlet ready={getViewportReady} /> |
|
|
{metadataOutlet} |
|
|
</OutletBoundary> |
|
|
</React.Fragment>, |
|
|
parallelRouteCacheNodeSeedData, |
|
|
loadingData, |
|
|
isPossiblyPartialResponse, |
|
|
] |
|
|
} else { |
|
|
const SegmentComponent = Component |
|
|
const isRootLayoutWithChildrenSlotAndAtLeastOneMoreSlot = |
|
|
rootLayoutAtThisLevel && |
|
|
'children' in parallelRoutes && |
|
|
Object.keys(parallelRoutes).length > 1 |
|
|
|
|
|
let segmentNode: React.ReactNode |
|
|
|
|
|
if (isClientComponent) { |
|
|
let clientSegment: React.ReactNode |
|
|
|
|
|
if (isStaticGeneration) { |
|
|
const promiseOfParams = |
|
|
createPrerenderParamsForClientSegment(currentParams) |
|
|
|
|
|
clientSegment = ( |
|
|
<ClientSegmentRoot |
|
|
Component={SegmentComponent} |
|
|
slots={parallelRouteProps} |
|
|
params={currentParams} |
|
|
promise={promiseOfParams} |
|
|
/> |
|
|
) |
|
|
} else { |
|
|
clientSegment = ( |
|
|
<ClientSegmentRoot |
|
|
Component={SegmentComponent} |
|
|
slots={parallelRouteProps} |
|
|
params={currentParams} |
|
|
/> |
|
|
) |
|
|
} |
|
|
|
|
|
if (isRootLayoutWithChildrenSlotAndAtLeastOneMoreSlot) { |
|
|
let notfoundClientSegment: React.ReactNode |
|
|
let forbiddenClientSegment: React.ReactNode |
|
|
let unauthorizedClientSegment: React.ReactNode |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
notfoundClientSegment = createErrorBoundaryClientSegmentRoot({ |
|
|
ErrorBoundaryComponent: NotFound, |
|
|
errorElement: notFoundElement, |
|
|
ClientSegmentRoot, |
|
|
layerAssets, |
|
|
SegmentComponent, |
|
|
currentParams, |
|
|
}) |
|
|
forbiddenClientSegment = createErrorBoundaryClientSegmentRoot({ |
|
|
ErrorBoundaryComponent: Forbidden, |
|
|
errorElement: forbiddenElement, |
|
|
ClientSegmentRoot, |
|
|
layerAssets, |
|
|
SegmentComponent, |
|
|
currentParams, |
|
|
}) |
|
|
unauthorizedClientSegment = createErrorBoundaryClientSegmentRoot({ |
|
|
ErrorBoundaryComponent: Unauthorized, |
|
|
errorElement: unauthorizedElement, |
|
|
ClientSegmentRoot, |
|
|
layerAssets, |
|
|
SegmentComponent, |
|
|
currentParams, |
|
|
}) |
|
|
if ( |
|
|
notfoundClientSegment || |
|
|
forbiddenClientSegment || |
|
|
unauthorizedClientSegment |
|
|
) { |
|
|
segmentNode = ( |
|
|
<HTTPAccessFallbackBoundary |
|
|
key={cacheNodeKey} |
|
|
notFound={notfoundClientSegment} |
|
|
forbidden={forbiddenClientSegment} |
|
|
unauthorized={unauthorizedClientSegment} |
|
|
> |
|
|
{layerAssets} |
|
|
{clientSegment} |
|
|
</HTTPAccessFallbackBoundary> |
|
|
) |
|
|
} else { |
|
|
segmentNode = ( |
|
|
<React.Fragment key={cacheNodeKey}> |
|
|
{layerAssets} |
|
|
{clientSegment} |
|
|
</React.Fragment> |
|
|
) |
|
|
} |
|
|
} else { |
|
|
segmentNode = ( |
|
|
<React.Fragment key={cacheNodeKey}> |
|
|
{layerAssets} |
|
|
{clientSegment} |
|
|
</React.Fragment> |
|
|
) |
|
|
} |
|
|
} else { |
|
|
const params = createServerParamsForServerSegment( |
|
|
currentParams, |
|
|
workStore |
|
|
) |
|
|
|
|
|
let serverSegment: React.ReactNode |
|
|
|
|
|
if (isUseCacheFunction(SegmentComponent)) { |
|
|
const UseCacheLayoutComponent: React.ComponentType<UseCacheLayoutComponentProps> = |
|
|
SegmentComponent |
|
|
|
|
|
serverSegment = ( |
|
|
<UseCacheLayoutComponent |
|
|
{...parallelRouteProps} |
|
|
params={params} |
|
|
$$isLayoutComponent |
|
|
/> |
|
|
) |
|
|
} else { |
|
|
serverSegment = ( |
|
|
<SegmentComponent {...parallelRouteProps} params={params} /> |
|
|
) |
|
|
} |
|
|
|
|
|
if (isRootLayoutWithChildrenSlotAndAtLeastOneMoreSlot) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
segmentNode = ( |
|
|
<HTTPAccessFallbackBoundary |
|
|
key={cacheNodeKey} |
|
|
notFound={ |
|
|
notFoundElement ? ( |
|
|
<> |
|
|
{layerAssets} |
|
|
<SegmentComponent params={params}> |
|
|
{notFoundStyles} |
|
|
{notFoundElement} |
|
|
</SegmentComponent> |
|
|
</> |
|
|
) : undefined |
|
|
} |
|
|
> |
|
|
{layerAssets} |
|
|
{serverSegment} |
|
|
</HTTPAccessFallbackBoundary> |
|
|
) |
|
|
} else { |
|
|
segmentNode = ( |
|
|
<React.Fragment key={cacheNodeKey}> |
|
|
{layerAssets} |
|
|
{serverSegment} |
|
|
</React.Fragment> |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
const layoutFilePath = getConventionPathByType(tree, dir, 'layout') |
|
|
const wrappedSegmentNode = |
|
|
isSegmentViewEnabled && layoutFilePath ? ( |
|
|
<SegmentViewNode key="layout" type="layout" pagePath={layoutFilePath}> |
|
|
{segmentNode} |
|
|
</SegmentViewNode> |
|
|
) : ( |
|
|
segmentNode |
|
|
) |
|
|
|
|
|
|
|
|
return [ |
|
|
actualSegment, |
|
|
wrappedSegmentNode, |
|
|
parallelRouteCacheNodeSeedData, |
|
|
loadingData, |
|
|
isPossiblyPartialResponse, |
|
|
] |
|
|
} |
|
|
} |
|
|
|
|
|
async function MetadataOutlet({ |
|
|
ready, |
|
|
}: { |
|
|
ready: () => Promise<void> & { status?: string; value?: unknown } |
|
|
}) { |
|
|
const r = ready() |
|
|
|
|
|
if (r.status === 'rejected') { |
|
|
throw r.value |
|
|
} else if (r.status !== 'fulfilled') { |
|
|
await r |
|
|
} |
|
|
return null |
|
|
} |
|
|
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 |
|
|
} |
|
|
|
|
|
export function getRootParams( |
|
|
loaderTree: LoaderTree, |
|
|
getDynamicParamFromSegment: GetDynamicParamFromSegment |
|
|
): Params { |
|
|
return getRootParamsImpl({}, loaderTree, getDynamicParamFromSegment) |
|
|
} |
|
|
|
|
|
function getRootParamsImpl( |
|
|
parentParams: Params, |
|
|
loaderTree: LoaderTree, |
|
|
getDynamicParamFromSegment: GetDynamicParamFromSegment |
|
|
): Params { |
|
|
const { |
|
|
segment, |
|
|
modules: { layout }, |
|
|
parallelRoutes, |
|
|
} = parseLoaderTree(loaderTree) |
|
|
|
|
|
const segmentParam = getDynamicParamFromSegment(segment) |
|
|
|
|
|
let currentParams: Params = parentParams |
|
|
if (segmentParam && segmentParam.value !== null) { |
|
|
currentParams = { |
|
|
...parentParams, |
|
|
[segmentParam.param]: segmentParam.value, |
|
|
} |
|
|
} |
|
|
|
|
|
const isRootLayout = typeof layout !== 'undefined' |
|
|
|
|
|
if (isRootLayout) { |
|
|
return currentParams |
|
|
} else if (!parallelRoutes.children) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return currentParams |
|
|
} else { |
|
|
return getRootParamsImpl( |
|
|
currentParams, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parallelRoutes.children, |
|
|
getDynamicParamFromSegment |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
async function createBoundaryConventionElement({ |
|
|
ctx, |
|
|
conventionName, |
|
|
Component, |
|
|
styles, |
|
|
tree, |
|
|
}: { |
|
|
ctx: AppRenderContext |
|
|
conventionName: |
|
|
| 'not-found' |
|
|
| 'error' |
|
|
| 'loading' |
|
|
| 'forbidden' |
|
|
| 'unauthorized' |
|
|
Component: React.ComponentType<any> | undefined |
|
|
styles: React.ReactNode | undefined |
|
|
tree: LoaderTree |
|
|
}) { |
|
|
const isSegmentViewEnabled = |
|
|
process.env.NODE_ENV === 'development' && |
|
|
ctx.renderOpts.devtoolSegmentExplorer |
|
|
const dir = |
|
|
process.env.NEXT_RUNTIME === 'edge' |
|
|
? process.env.__NEXT_EDGE_PROJECT_DIR! |
|
|
: ctx.renderOpts.dir || '' |
|
|
const { SegmentViewNode } = ctx.componentMod |
|
|
const element = Component ? ( |
|
|
<> |
|
|
<Component /> |
|
|
{styles} |
|
|
</> |
|
|
) : undefined |
|
|
|
|
|
const pagePath = getConventionPathByType(tree, dir, conventionName) |
|
|
|
|
|
const wrappedElement = |
|
|
isSegmentViewEnabled && element ? ( |
|
|
<SegmentViewNode |
|
|
key={cacheNodeKey + '-' + conventionName} |
|
|
type={conventionName} |
|
|
pagePath={pagePath!} |
|
|
> |
|
|
{element} |
|
|
</SegmentViewNode> |
|
|
) : ( |
|
|
element |
|
|
) |
|
|
|
|
|
return [wrappedElement, pagePath] as const |
|
|
} |
|
|
|