Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app-router: update prefetch heuristic to traverse until until we hit a loading boundary #49077

Merged
80 changes: 38 additions & 42 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,8 @@ export async function renderToHTMLOrFlight(
const childSegment = parallelRoute[0]
const childSegmentParam = getDynamicParamFromSegment(childSegment)

// if we're prefetching and that there's a Loading component, we bail out
// otherwise we keep rendering for the prefetch
if (isPrefetch && Loading) {
const childProp: ChildProp = {
// Null indicates the tree is not fully rendered
Expand Down Expand Up @@ -1025,48 +1027,42 @@ export async function renderToHTMLOrFlight(
getDynamicParamFromSegment,
query
),
// Check if one level down from the common layout has a loading component. If it doesn't only provide the router state as part of the Flight data.
isPrefetch && !Boolean(components.loading)
? null
: // Create component tree using the slice of the loaderTree
// @ts-expect-error TODO-APP: fix async component type
React.createElement(async () => {
const { Component } = await createComponentTree(
// This ensures flightRouterPath is valid and filters down the tree
{
createSegmentPath,
loaderTree: loaderTreeToFilter,
parentParams: currentParams,
firstItem: isFirst,
injectedCSS,
injectedFontPreloadTags,
// This is intentionally not "rootLayoutIncludedAtThisLevelOrAbove" as createComponentTree starts at the current level and does a check for "rootLayoutAtThisLevel" too.
rootLayoutIncluded,
asNotFound,
}
)

return <Component />
}),
isPrefetch && !Boolean(components.loading)
? null
: (() => {
const { layoutOrPagePath } =
parseLoaderTree(loaderTreeToFilter)

const styles = getLayerAssets({
layoutOrPagePath,
injectedCSS: new Set(injectedCSS),
injectedFontPreloadTags: new Set(injectedFontPreloadTags),
})

return (
<>
{styles}
{rscPayloadHead}
</>
)
})(),
// Create component tree using the slice of the loaderTree
// @ts-expect-error TODO-APP: fix async component type
React.createElement(async () => {
const { Component } = await createComponentTree(
// This ensures flightRouterPath is valid and filters down the tree
{
createSegmentPath,
loaderTree: loaderTreeToFilter,
parentParams: currentParams,
firstItem: isFirst,
injectedCSS,
injectedFontPreloadTags,
// This is intentionally not "rootLayoutIncludedAtThisLevelOrAbove" as createComponentTree starts at the current level and does a check for "rootLayoutAtThisLevel" too.
rootLayoutIncluded,
asNotFound,
}
)

return <Component />
}),
(() => {
const { layoutOrPagePath } = parseLoaderTree(loaderTreeToFilter)

const styles = getLayerAssets({
layoutOrPagePath,
injectedCSS: new Set(injectedCSS),
injectedFontPreloadTags: new Set(injectedFontPreloadTags),
})

return (
<>
{styles}
{rscPayloadHead}
</>
)
})(),
],
]
}
Expand Down