Unverified Commit c5468474 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

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

This PR changes the router behaviour during prefetching.

- before: the router would prefetch the router tree + return the loading state of new router node if there were any
- after: the router will prefetch the router tree + prefetch the content of the new page up until the nearest loading boundaries

Example:

Note: `prefetched` here implies React content prefetching

```
.
└── app/
    ├── page.ts <- the page you're on, has links to /about and /post/[id]
    ├── layout.ts
    ├── post/
    │   └── [id]/
    │       ├── (comments)/
    │       │   ├── loading.ts <- not prefetched
    │       │   └── page.ts <- not prefetched
    │       ├── layout.ts
    │       └── page.ts <- not prefetched
    └── about/
        └── page.ts <- not prefetched
```


After
```
.
└── app/
    ├── page.ts <- the page you're on, has links to /about and /post/[id]
    ├── layout.ts
    ├── post/
    │   └── [id]/
    │       ├── (comments)/
    │       │   ├── loading.ts <- prefetched
    │       │   └── page.ts <- not prefetched
    │       ├── layout.ts
    │       └── page.ts <- prefetched
    └── about/
        └── page.ts <- prefetched
```


link NEXT-1078
parent 8d9658c6
...@@ -752,6 +752,8 @@ export async function renderToHTMLOrFlight( ...@@ -752,6 +752,8 @@ export async function renderToHTMLOrFlight(
const childSegment = parallelRoute[0] const childSegment = parallelRoute[0]
const childSegmentParam = getDynamicParamFromSegment(childSegment) 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) { if (isPrefetch && Loading) {
const childProp: ChildProp = { const childProp: ChildProp = {
// Null indicates the tree is not fully rendered // Null indicates the tree is not fully rendered
...@@ -1025,48 +1027,42 @@ export async function renderToHTMLOrFlight( ...@@ -1025,48 +1027,42 @@ export async function renderToHTMLOrFlight(
getDynamicParamFromSegment, getDynamicParamFromSegment,
query 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. // Create component tree using the slice of the loaderTree
isPrefetch && !Boolean(components.loading) // @ts-expect-error TODO-APP: fix async component type
? null React.createElement(async () => {
: // Create component tree using the slice of the loaderTree const { Component } = await createComponentTree(
// @ts-expect-error TODO-APP: fix async component type // This ensures flightRouterPath is valid and filters down the tree
React.createElement(async () => { {
const { Component } = await createComponentTree( createSegmentPath,
// This ensures flightRouterPath is valid and filters down the tree loaderTree: loaderTreeToFilter,
{ parentParams: currentParams,
createSegmentPath, firstItem: isFirst,
loaderTree: loaderTreeToFilter, injectedCSS,
parentParams: currentParams, injectedFontPreloadTags,
firstItem: isFirst, // This is intentionally not "rootLayoutIncludedAtThisLevelOrAbove" as createComponentTree starts at the current level and does a check for "rootLayoutAtThisLevel" too.
injectedCSS, rootLayoutIncluded,
injectedFontPreloadTags, asNotFound,
// This is intentionally not "rootLayoutIncludedAtThisLevelOrAbove" as createComponentTree starts at the current level and does a check for "rootLayoutAtThisLevel" too. }
rootLayoutIncluded, )
asNotFound,
} return <Component />
) }),
(() => {
return <Component /> const { layoutOrPagePath } = parseLoaderTree(loaderTreeToFilter)
}),
isPrefetch && !Boolean(components.loading) const styles = getLayerAssets({
? null layoutOrPagePath,
: (() => { injectedCSS: new Set(injectedCSS),
const { layoutOrPagePath } = injectedFontPreloadTags: new Set(injectedFontPreloadTags),
parseLoaderTree(loaderTreeToFilter) })
const styles = getLayerAssets({ return (
layoutOrPagePath, <>
injectedCSS: new Set(injectedCSS), {styles}
injectedFontPreloadTags: new Set(injectedFontPreloadTags), {rscPayloadHead}
}) </>
)
return ( })(),
<>
{styles}
{rscPayloadHead}
</>
)
})(),
], ],
] ]
} }
......
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