Unverified Commit 1734e91a authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

fix loading bailout condition & server patch mismatch tolerance (#72227)

Pre-PPR on client navigations, we are currently determining whether or
not to render the underlying React components based on the presence (or
lack thereof) of `loading.js`. We'll render components up to the first
segment containing `loading.js`, and then stop at that level.

Similarly, if there's no `loading.js` in the tree at all, we'll return a
mostly empty prefetch. The underlying reason for this is that we cannot
assume a particular tree/subtree doesn't contain expensive invocations:
`loading.js` is a signal we use to detect where some sort of dynamic
call will happen. This heuristic is largely improved with PPR + DIO but
we still need to support this case.

Currently there's a bug that will lead to overfetching. When determining
if we should skip the component tree, we check the presence of loading
at that level, as well as the entire tree. This means that during
partial navigations when the server starts rendering deeper within the
tree rather than starting from the root, the presence of `loading.js` in
an earlier segment would incorrectly tell the router it should render
the page.

This updates the handling to check for a loading segment only within the
partial tree that's being diffed. That way when we recursively call
this, it won't look at earlier segments when deciding if there's a
loading segment deeper in the tree.

This PR also adjusts the server-patch reducer to be more resilient to
mismatching trees. Previously we triggered a hard navigation but this
actually can become a fairly common case if the RSC response doesn't
return initial bytes quickly enough, such as in `dynamicIO` when we're
warming the dev caches prior to a navigation. Instead we'll return the
state in-place and effectively discard the patch because the assumption
is the tree was patched elsewhere.
parent 926752ec
...@@ -12,7 +12,6 @@ import { applyFlightData } from '../apply-flight-data' ...@@ -12,7 +12,6 @@ import { applyFlightData } from '../apply-flight-data'
import { handleMutable } from '../handle-mutable' import { handleMutable } from '../handle-mutable'
import type { CacheNode } from '../../../../shared/lib/app-router-context.shared-runtime' import type { CacheNode } from '../../../../shared/lib/app-router-context.shared-runtime'
import { createEmptyCacheNode } from '../../app-router' import { createEmptyCacheNode } from '../../app-router'
import { handleSegmentMismatch } from '../handle-segment-mismatch'
export function serverPatchReducer( export function serverPatchReducer(
state: ReadonlyReducerState, state: ReadonlyReducerState,
...@@ -51,8 +50,13 @@ export function serverPatchReducer( ...@@ -51,8 +50,13 @@ export function serverPatchReducer(
state.canonicalUrl state.canonicalUrl
) )
// `applyRouterStatePatchToTree` returns `null` when it determined that the server response is not applicable to the current tree.
// In other words, the server responded with a tree that doesn't match what the client is currently rendering.
// This can happen if the server patch action took longer to resolve than a subsequent navigation which would have changed the tree.
// Previously this case triggered an MPA navigation but it should be safe to simply discard the server response rather than forcing
// the entire page to reload.
if (newTree === null) { if (newTree === null) {
return handleSegmentMismatch(state, action, treePatch) return state
} }
if (isNavigatingToNewRootLayout(currentTree, newTree)) { if (isNavigatingToNewRootLayout(currentTree, newTree)) {
......
...@@ -62,7 +62,6 @@ export async function walkTreeWithFlightRouterState({ ...@@ -62,7 +62,6 @@ export async function walkTreeWithFlightRouterState({
query, query,
isPrefetch, isPrefetch,
getDynamicParamFromSegment, getDynamicParamFromSegment,
componentMod: { tree: loaderTree },
} = ctx } = ctx
const [segment, parallelRoutes, modules] = loaderTreeToFilter const [segment, parallelRoutes, modules] = loaderTreeToFilter
...@@ -118,7 +117,7 @@ export async function walkTreeWithFlightRouterState({ ...@@ -118,7 +117,7 @@ export async function walkTreeWithFlightRouterState({
!experimental.isRoutePPREnabled && !experimental.isRoutePPREnabled &&
isPrefetch && isPrefetch &&
!Boolean(modules.loading) && !Boolean(modules.loading) &&
!hasLoadingComponentInTree(loaderTree) !hasLoadingComponentInTree(loaderTreeToFilter)
if (!parentRendered && renderComponentsOnThisLevel) { if (!parentRendered && renderComponentsOnThisLevel) {
const overriddenSegment = const overriddenSegment =
......
...@@ -232,7 +232,7 @@ describe('app dir - prefetching', () => { ...@@ -232,7 +232,7 @@ describe('app dir - prefetching', () => {
}) })
const prefetchResponse = await response.text() const prefetchResponse = await response.text()
expect(prefetchResponse).toContain('Page Data!') expect(prefetchResponse).not.toContain('Page Data!')
expect(prefetchResponse).not.toContain('Layout Data!') expect(prefetchResponse).not.toContain('Layout Data!')
expect(prefetchResponse).not.toContain('Loading Prefetch Auto') expect(prefetchResponse).not.toContain('Loading Prefetch Auto')
}) })
......
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