Unverified Commit c30747fe authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Skip empty prefetch request for dynamic routes (#78436)

In the Segment Cache implementation of prefetching, a prefetch happens
in two phases: first we fetch the route tree, then we fetch the data
itself.

The route tree prefetch tells us important metadata about the structure
of the target route, like whether it shares any layouts with the current
page. We can use this information to omit parts of the tree from
subsequent data request.

In this PR, I've added a new piece of metadata to the route tree
prefetch response: `hasLoadingBoundary`. For each segment in the tree,
this represents whether there is a `loading.tsx` boundary somewhere
within it. This is useful because we know that during a prefetch, the
server will only render up to the nearest loading boundary (unless
prefetch={true} is set); if there are no loading boundaries, then the
server will return an empty response.

In the latter case, we can use this information to skip the data
prefetch entirely, freeing up bandwidth for other requests in the queue.
parent d35f80b8
...@@ -11,6 +11,7 @@ import type { ...@@ -11,6 +11,7 @@ import type {
CacheNodeSeedData, CacheNodeSeedData,
Segment as FlightRouterStateSegment, Segment as FlightRouterStateSegment,
} from '../../../server/app-render/types' } from '../../../server/app-render/types'
import { HasLoadingBoundary } from '../../../server/app-render/types'
import { import {
NEXT_DID_POSTPONE_HEADER, NEXT_DID_POSTPONE_HEADER,
NEXT_ROUTER_PREFETCH_HEADER, NEXT_ROUTER_PREFETCH_HEADER,
...@@ -85,6 +86,13 @@ export type RouteTree = { ...@@ -85,6 +86,13 @@ export type RouteTree = {
[parallelRouteKey: string]: RouteTree [parallelRouteKey: string]: RouteTree
} }
isRootLayout: boolean isRootLayout: boolean
// If this is a dynamic route, indicates whether there is a loading boundary
// somewhere in the tree. If not, we can skip the prefetch for the data,
// because we know it would be an empty response. (For a static/PPR route,
// this value is disregarded, because in that model `loading.tsx` is treated
// like any other Suspense boundary.)
hasLoadingBoundary: HasLoadingBoundary
} }
type RouteCacheEntryShared = { type RouteCacheEntryShared = {
...@@ -871,6 +879,9 @@ function convertTreePrefetchToRouteTree( ...@@ -871,6 +879,9 @@ function convertTreePrefetchToRouteTree(
segment: prefetch.segment, segment: prefetch.segment,
slots, slots,
isRootLayout: prefetch.isRootLayout, isRootLayout: prefetch.isRootLayout,
// This field is only relevant to dynamic routes. For a PPR/static route,
// there's always some partial loading state we can fetch.
hasLoadingBoundary: HasLoadingBoundary.SegmentHasLoadingBoundary,
} }
} }
...@@ -935,6 +946,10 @@ function convertFlightRouterStateToRouteTree( ...@@ -935,6 +946,10 @@ function convertFlightRouterStateToRouteTree(
segment: segmentWithoutSearchParams, segment: segmentWithoutSearchParams,
slots, slots,
isRootLayout: flightRouterState[4] === true, isRootLayout: flightRouterState[4] === true,
hasLoadingBoundary:
flightRouterState[5] !== undefined
? flightRouterState[5]
: HasLoadingBoundary.SubtreeHasNoLoadingBoundary,
} }
} }
......
...@@ -2,6 +2,7 @@ import type { ...@@ -2,6 +2,7 @@ import type {
FlightRouterState, FlightRouterState,
Segment as FlightRouterStateSegment, Segment as FlightRouterStateSegment,
} from '../../../server/app-render/types' } from '../../../server/app-render/types'
import { HasLoadingBoundary } from '../../../server/app-render/types'
import { matchSegment } from '../match-segments' import { matchSegment } from '../match-segments'
import { import {
readOrCreateRouteCacheEntry, readOrCreateRouteCacheEntry,
...@@ -684,20 +685,23 @@ function diffRouteTreeAgainstCurrent( ...@@ -684,20 +685,23 @@ function diffRouteTreeAgainstCurrent(
// conservative about which segments to include in the request. // conservative about which segments to include in the request.
// //
// The server will only render up to the first loading boundary // The server will only render up to the first loading boundary
// inside new part of the tree. If there's no loading boundary, the // inside new part of the tree. If there's no loading boundary
// server will never return any data. TODO: When we prefetch the // anywhere in the tree, the server will never return any data, so
// route tree, the server should indicate whether there's a loading // we can skip the request.
// boundary so the client doesn't send a second request for no const subtreeHasLoadingBoundary =
// reason. newTreeChild.hasLoadingBoundary !==
const requestTreeChild = HasLoadingBoundary.SubtreeHasNoLoadingBoundary
pingPPRDisabledRouteTreeUpToLoadingBoundary( const requestTreeChild = subtreeHasLoadingBoundary
now, ? pingPPRDisabledRouteTreeUpToLoadingBoundary(
task, now,
route, task,
newTreeChild, route,
null, newTreeChild,
spawnedEntries null,
) spawnedEntries
)
: // There's no loading boundary within this tree. Bail out.
convertRouteTreeToFlightRouterState(newTreeChild)
requestTreeChildren[parallelRouteKey] = requestTreeChild requestTreeChildren[parallelRouteKey] = requestTreeChild
break break
} }
...@@ -800,9 +804,9 @@ function pingPPRDisabledRouteTreeUpToLoadingBoundary( ...@@ -800,9 +804,9 @@ function pingPPRDisabledRouteTreeUpToLoadingBoundary(
} }
case EntryStatus.Fulfilled: { case EntryStatus.Fulfilled: {
// The segment is already cached. // The segment is already cached.
// TODO: The server should include a `hasLoading` field as part of the const segmentHasLoadingBoundary =
// route tree prefetch. tree.hasLoadingBoundary === HasLoadingBoundary.SegmentHasLoadingBoundary
if (segment.loading !== null) { if (segmentHasLoadingBoundary) {
// This segment has a loading boundary, which means the server won't // This segment has a loading boundary, which means the server won't
// render its children. So there's nothing left to prefetch along this // render its children. So there's nothing left to prefetch along this
// path. We can bail out. // path. We can bail out.
......
import type { LoaderTree } from '../lib/app-dir-module' import type { LoaderTree } from '../lib/app-dir-module'
import type { FlightRouterState } from './types' import { HasLoadingBoundary, type FlightRouterState } from './types'
import type { GetDynamicParamFromSegment } from './app-render' import type { GetDynamicParamFromSegment } from './app-render'
import { addSearchParamsIfPageSegment } from '../../shared/lib/segment' import { addSearchParamsIfPageSegment } from '../../shared/lib/segment'
export function createFlightRouterStateFromLoaderTree( function createFlightRouterStateFromLoaderTreeImpl(
[segment, parallelRoutes, { layout }]: LoaderTree, [segment, parallelRoutes, { layout, loading }]: LoaderTree,
getDynamicParamFromSegment: GetDynamicParamFromSegment, getDynamicParamFromSegment: GetDynamicParamFromSegment,
searchParams: any, searchParams: any,
rootLayoutIncluded = false includeHasLoadingBoundary: boolean,
didFindRootLayout: boolean
): FlightRouterState { ): FlightRouterState {
const dynamicParam = getDynamicParamFromSegment(segment) const dynamicParam = getDynamicParamFromSegment(segment)
const treeSegment = dynamicParam ? dynamicParam.treeSegment : segment const treeSegment = dynamicParam ? dynamicParam.treeSegment : segment
...@@ -17,23 +18,83 @@ export function createFlightRouterStateFromLoaderTree( ...@@ -17,23 +18,83 @@ export function createFlightRouterStateFromLoaderTree(
{}, {},
] ]
if (!rootLayoutIncluded && typeof layout !== 'undefined') { // Mark the first segment that has a layout as the "root" layout
rootLayoutIncluded = true if (!didFindRootLayout && typeof layout !== 'undefined') {
didFindRootLayout = true
segmentTree[4] = true segmentTree[4] = true
} }
segmentTree[1] = Object.keys(parallelRoutes).reduce( let childHasLoadingBoundary = false
(existingValue, currentValue) => { const children: FlightRouterState[1] = {}
existingValue[currentValue] = createFlightRouterStateFromLoaderTree( Object.keys(parallelRoutes).forEach((parallelRouteKey) => {
parallelRoutes[currentValue], const child = createFlightRouterStateFromLoaderTreeImpl(
getDynamicParamFromSegment, parallelRoutes[parallelRouteKey],
searchParams, getDynamicParamFromSegment,
rootLayoutIncluded searchParams,
) includeHasLoadingBoundary,
return existingValue didFindRootLayout
}, )
{} as FlightRouterState[1] if (
) includeHasLoadingBoundary &&
child[5] !== HasLoadingBoundary.SubtreeHasNoLoadingBoundary
) {
childHasLoadingBoundary = true
}
children[parallelRouteKey] = child
})
segmentTree[1] = children
if (includeHasLoadingBoundary) {
// During a route tree prefetch, the FlightRouterState includes whether a
// tree has a loading boundary. The client uses this to determine if it can
// skip the data prefetch request — if `hasLoadingBoundary` is `false`, the
// data prefetch response will be empty, so there's no reason to request it.
// NOTE: It would be better to accumulate this while building the loader
// tree so we don't have to keep re-deriving it, but since this won't be
// once PPR is enabled everywhere, it's not that important.
segmentTree[5] = loading
? HasLoadingBoundary.SegmentHasLoadingBoundary
: childHasLoadingBoundary
? HasLoadingBoundary.SubtreeHasLoadingBoundary
: HasLoadingBoundary.SubtreeHasNoLoadingBoundary
}
return segmentTree return segmentTree
} }
export function createFlightRouterStateFromLoaderTree(
loaderTree: LoaderTree,
getDynamicParamFromSegment: GetDynamicParamFromSegment,
searchParams: any
) {
const includeHasLoadingBoundary = false
const didFindRootLayout = false
return createFlightRouterStateFromLoaderTreeImpl(
loaderTree,
getDynamicParamFromSegment,
searchParams,
includeHasLoadingBoundary,
didFindRootLayout
)
}
export function createRouteTreePrefetch(
loaderTree: LoaderTree,
getDynamicParamFromSegment: GetDynamicParamFromSegment
): FlightRouterState {
// Search params should not be added to page segment's cache key during a
// route tree prefetch request, because they do not affect the structure of
// the route. The client cache has its own logic to handle search params.
const searchParams = {}
// During a route tree prefetch, we include `hasLoadingBoundary` in
// the response.
const includeHasLoadingBoundary = true
const didFindRootLayout = false
return createFlightRouterStateFromLoaderTreeImpl(
loaderTree,
getDynamicParamFromSegment,
searchParams,
includeHasLoadingBoundary,
didFindRootLayout
)
}
...@@ -94,8 +94,24 @@ export type FlightRouterState = [ ...@@ -94,8 +94,24 @@ export type FlightRouterState = [
*/ */
refresh?: 'refetch' | 'refresh' | 'inside-shared-layout' | null, refresh?: 'refetch' | 'refresh' | 'inside-shared-layout' | null,
isRootLayout?: boolean, isRootLayout?: boolean,
/**
* Only present when responding to a tree prefetch request. Indicates whether
* there is a loading boundary somewhere in the tree. The client cache uses
* this to determine if it can skip the data prefetch request.
*/
hasLoadingBoundary?: HasLoadingBoundary,
] ]
export const enum HasLoadingBoundary {
// There is a loading boundary in this particular segment
SegmentHasLoadingBoundary = 1,
// There is a loading boundary somewhere in the subtree (but not in
// this segment)
SubtreeHasLoadingBoundary = 2,
// There is no loading boundary in this segment or any of its descendants
SubtreeHasNoLoadingBoundary = 3,
}
/** /**
* Individual Flight response path * Individual Flight response path
*/ */
......
...@@ -9,7 +9,10 @@ import { matchSegment } from '../../client/components/match-segments' ...@@ -9,7 +9,10 @@ import { matchSegment } from '../../client/components/match-segments'
import type { LoaderTree } from '../lib/app-dir-module' import type { LoaderTree } from '../lib/app-dir-module'
import { getLinkAndScriptTags } from './get-css-inlined-link-tags' import { getLinkAndScriptTags } from './get-css-inlined-link-tags'
import { getPreloadableFonts } from './get-preloadable-fonts' import { getPreloadableFonts } from './get-preloadable-fonts'
import { createFlightRouterStateFromLoaderTree } from './create-flight-router-state-from-loader-tree' import {
createFlightRouterStateFromLoaderTree,
createRouteTreePrefetch,
} from './create-flight-router-state-from-loader-tree'
import type { AppRenderContext } from './app-render' import type { AppRenderContext } from './app-render'
import { hasLoadingComponentInTree } from './has-loading-component-in-tree' import { hasLoadingComponentInTree } from './has-loading-component-in-tree'
import { import {
...@@ -153,12 +156,15 @@ export async function walkTreeWithFlightRouterState({ ...@@ -153,12 +156,15 @@ export async function walkTreeWithFlightRouterState({
? flightRouterState[0] ? flightRouterState[0]
: actualSegment : actualSegment
const routerState = createFlightRouterStateFromLoaderTree( const routerState = parsedRequestHeaders.isRouteTreePrefetchRequest
// Create router state using the slice of the loaderTree ? // Route tree prefetch requests contain some extra information
loaderTreeToFilter, createRouteTreePrefetch(loaderTreeToFilter, getDynamicParamFromSegment)
getDynamicParamFromSegment, : createFlightRouterStateFromLoaderTree(
query loaderTreeToFilter,
) getDynamicParamFromSegment,
query
)
return [ return [
[ [
overriddenSegment, overriddenSegment,
......
...@@ -35,6 +35,13 @@ export default function Page() { ...@@ -35,6 +35,13 @@ export default function Page() {
<LinkAccordion href="/ppr-disabled-with-loading-boundary"> <LinkAccordion href="/ppr-disabled-with-loading-boundary">
Page with PPR disabled, but has a loading boundary Page with PPR disabled, but has a loading boundary
</LinkAccordion> </LinkAccordion>
<ul>
<li>
<LinkAccordion href="/ppr-disabled-with-loading-boundary/child">
Another dynamic page that shares the same loading boundary
</LinkAccordion>
</li>
</ul>
</li> </li>
</ul> </ul>
</> </>
......
import { connection } from 'next/server'
export default async function PPRDisabledWithLoadingBoundaryChildPage() {
await connection()
return <div id="child-page-content">Child page content</div>
}
import { Suspense } from 'react' export default function Loading() {
import { connection } from 'next/server' return <div id="loading-boundary">Loading...</div>
async function Content() {
await connection()
return 'Dynamic Content'
}
export default function PPRDisabled() {
return (
<Suspense fallback="Loading...">
<Content />
</Suspense>
)
} }
...@@ -92,6 +92,140 @@ describe('segment cache (incremental opt in)', () => { ...@@ -92,6 +92,140 @@ describe('segment cache (incremental opt in)', () => {
testPrefetchDeduping('/ppr-disabled-with-loading-boundary')) testPrefetchDeduping('/ppr-disabled-with-loading-boundary'))
}) })
it('prefetches a dynamic route when PPR is disabled if it has a loading.tsx boundary', async () => {
let act
const browser = await next.browser('/', {
beforePageLoad(p) {
act = createRouterAct(p)
},
})
// Initiate a prefetch for a dynamic page with no PPR, but with a
// loading.tsx boundary. We should be able to prefetch up to the
// loading boundary.
await act(async () => {
const checkbox = await browser.elementByCss(
`input[data-link-accordion="/ppr-disabled-with-loading-boundary"]`
)
await checkbox.click()
}, [
// Response includes the loading boundary
{ includes: 'Loading...' },
// but it does not include the dynamic page content
{ includes: 'Page content', block: 'reject' },
])
// Navigate to the page
await act(
async () => {
await browser
.elementByCss(`a[href="/ppr-disabled-with-loading-boundary"]`)
.click()
// We can immediately render the loading boundary before the network
// responds, because it was prefetched
const loadingBoundary = await browser.elementById('loading-boundary')
expect(await loadingBoundary.text()).toBe('Loading...')
},
// The page content is fetched on navigation
{ includes: 'Page content' }
)
const pageContent = await browser.elementById('page-content')
expect(await pageContent.text()).toBe('Page content')
})
it(
'skips prefetching a dynamic route when PPR is disabled if everything up ' +
'to its loading.tsx boundary is already cached',
async () => {
let act
const browser = await next.browser('/', {
beforePageLoad(p) {
act = createRouterAct(p)
},
})
// Initiate a prefetch for a dynamic page with no PPR, but with a
// loading.tsx boundary. We should be able to prefetch up to the
// loading boundary.
await act(async () => {
const checkbox = await browser.elementByCss(
`input[data-link-accordion="/ppr-disabled-with-loading-boundary"]`
)
await checkbox.click()
}, [
// Response includes the loading boundary
{ includes: 'Loading...' },
// but it does not include the dynamic page content
{ includes: 'Page content', block: 'reject' },
])
await act(async () => {
const checkbox = await browser.elementByCss(
`input[data-link-accordion="/ppr-disabled-with-loading-boundary/child"]`
)
await checkbox.click()
}, 'no-requests')
// Navigate to the page
await act(async () => {
await browser
.elementByCss(`a[href="/ppr-disabled-with-loading-boundary/child"]`)
.click()
// We can immediately render the loading boundary before the network
// responds, because it was prefetched
const loadingBoundary = await browser.elementById('loading-boundary')
expect(await loadingBoundary.text()).toBe('Loading...')
}, [
// The page content is fetched on navigation
{ includes: 'Child page content' },
// The loading boundary is not fetched on navigation, because it
// was already loaded into the cache during the prefetch for the
// other page.
{ includes: 'Loading...', block: 'reject' },
])
const pageContent = await browser.elementById('child-page-content')
expect(await pageContent.text()).toBe('Child page content')
}
)
it('skips prefetching a dynamic route when PPR is disabled if it has no loading.tsx boundary', async () => {
let act
const browser = await next.browser('/', {
beforePageLoad(p) {
act = createRouterAct(p)
},
})
// Initiate a prefetch for a dynamic page with no loading.tsx and no PPR.
// The route tree prefetch will tell the client that there is no loading
// boundary, and therefore the client can skip prefetching the data, since
// it would be an empty response, anyway.
await act(
async () => {
const checkbox = await browser.elementByCss(
`input[data-link-accordion="/ppr-disabled"]`
)
await checkbox.click()
},
// This assertion will fail if more than one request includes the given
// string. Because the string appears in the FlightRouterState for the
// page, it effectively asserts that only one prefetch request is issued
// — the one for the route tree.
{ includes: 'ppr-disabled' }
)
// Navigate to the page
await act(
async () => {
await browser.elementByCss(`a[href="/ppr-disabled"]`).click()
},
// The page content is fetched on navigation
{ includes: 'Page content' }
)
const pageContent = await browser.elementById('page-content')
expect(await pageContent.text()).toBe('Page content')
})
it( it(
'prefetches a shared layout on a PPR-enabled route that was previously ' + 'prefetches a shared layout on a PPR-enabled route that was previously ' +
'omitted from a non-PPR-enabled route', 'omitted from a non-PPR-enabled route',
......
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