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,10 +1027,7 @@ export async function renderToHTMLOrFlight( ...@@ -1025,10 +1027,7 @@ 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)
? null
: // Create component tree using the slice of the loaderTree
// @ts-expect-error TODO-APP: fix async component type // @ts-expect-error TODO-APP: fix async component type
React.createElement(async () => { React.createElement(async () => {
const { Component } = await createComponentTree( const { Component } = await createComponentTree(
...@@ -1048,11 +1047,8 @@ export async function renderToHTMLOrFlight( ...@@ -1048,11 +1047,8 @@ export async function renderToHTMLOrFlight(
return <Component /> return <Component />
}), }),
isPrefetch && !Boolean(components.loading) (() => {
? null const { layoutOrPagePath } = parseLoaderTree(loaderTreeToFilter)
: (() => {
const { layoutOrPagePath } =
parseLoaderTree(loaderTreeToFilter)
const styles = getLayerAssets({ const styles = getLayerAssets({
layoutOrPagePath, layoutOrPagePath,
......
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