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

Allow missing CacheNodeSeedData during prefetch (#72857)

Updates the client PPR implementation to gracefully handle missing
CacheNodeSeedData when constructing a prerender tree. Currently this
never happens because we always block until we receive a server
response. However, in the new Segment Cache, we will not bother to wait
if a URL has not already been prefetched, since it's usually faster to
go straight to the dynamic request.
parent b5f5495b
...@@ -63,13 +63,13 @@ export function updateCacheNodeOnNavigation( ...@@ -63,13 +63,13 @@ export function updateCacheNodeOnNavigation(
oldCacheNode: CacheNode, oldCacheNode: CacheNode,
oldRouterState: FlightRouterState, oldRouterState: FlightRouterState,
newRouterState: FlightRouterState, newRouterState: FlightRouterState,
prefetchData: CacheNodeSeedData, prefetchData: CacheNodeSeedData | null,
prefetchHead: React.ReactNode prefetchHead: React.ReactNode | null
): Task | null { ): Task | null {
// Diff the old and new trees to reuse the shared layouts. // Diff the old and new trees to reuse the shared layouts.
const oldRouterStateChildren = oldRouterState[1] const oldRouterStateChildren = oldRouterState[1]
const newRouterStateChildren = newRouterState[1] const newRouterStateChildren = newRouterState[1]
const prefetchDataChildren = prefetchData[2] const prefetchDataChildren = prefetchData !== null ? prefetchData[2] : null
const oldParallelRoutes = oldCacheNode.parallelRoutes const oldParallelRoutes = oldCacheNode.parallelRoutes
...@@ -113,7 +113,9 @@ export function updateCacheNodeOnNavigation( ...@@ -113,7 +113,9 @@ export function updateCacheNodeOnNavigation(
oldRouterStateChildren[parallelRouteKey] oldRouterStateChildren[parallelRouteKey]
const oldSegmentMapChild = oldParallelRoutes.get(parallelRouteKey) const oldSegmentMapChild = oldParallelRoutes.get(parallelRouteKey)
const prefetchDataChild: CacheNodeSeedData | void | null = const prefetchDataChild: CacheNodeSeedData | void | null =
prefetchDataChildren[parallelRouteKey] prefetchDataChildren !== null
? prefetchDataChildren[parallelRouteKey]
: null
const newSegmentChild = newRouterStateChild[0] const newSegmentChild = newRouterStateChild[0]
const newSegmentKeyChild = createRouterCacheKey(newSegmentChild) const newSegmentKeyChild = createRouterCacheKey(newSegmentChild)
...@@ -156,24 +158,15 @@ export function updateCacheNodeOnNavigation( ...@@ -156,24 +158,15 @@ export function updateCacheNodeOnNavigation(
oldCacheNodeChild !== undefined && oldCacheNodeChild !== undefined &&
oldRouterStateChild !== undefined oldRouterStateChild !== undefined
) { ) {
// This segment exists in both the old and new trees. // This segment exists in both the old and new trees. Recursively update
if (prefetchDataChild !== undefined && prefetchDataChild !== null) { // the children.
// Recursively update the children. taskChild = updateCacheNodeOnNavigation(
taskChild = updateCacheNodeOnNavigation( oldCacheNodeChild,
oldCacheNodeChild, oldRouterStateChild,
oldRouterStateChild, newRouterStateChild,
newRouterStateChild, prefetchDataChild,
prefetchDataChild, prefetchHead
prefetchHead )
)
} else {
// The server didn't send any prefetch data for this segment. This
// shouldn't happen because the Route Tree and the Seed Data tree
// should always be the same shape, but until we unify those types
// it's still possible. For now we're going to deopt and trigger a
// lazy fetch during render.
taskChild = spawnTaskForMissingData(newRouterStateChild)
}
} else { } else {
// Either there's no existing Cache Node for this segment, or this // Either there's no existing Cache Node for this segment, or this
// segment doesn't exist in the old Router State tree. Switch to the // segment doesn't exist in the old Router State tree. Switch to the
...@@ -278,7 +271,7 @@ function patchRouterStateWithNewChildren( ...@@ -278,7 +271,7 @@ function patchRouterStateWithNewChildren(
function spawnPendingTask( function spawnPendingTask(
routerState: FlightRouterState, routerState: FlightRouterState,
prefetchData: CacheNodeSeedData | null, prefetchData: CacheNodeSeedData | null,
prefetchHead: React.ReactNode prefetchHead: React.ReactNode | null
): Task { ): Task {
// Create a task that will later be fulfilled by data from the server. // Create a task that will later be fulfilled by data from the server.
const pendingCacheNode = createPendingCacheNode( const pendingCacheNode = createPendingCacheNode(
...@@ -303,18 +296,6 @@ function spawnReusedTask(reusedRouterState: FlightRouterState): Task { ...@@ -303,18 +296,6 @@ function spawnReusedTask(reusedRouterState: FlightRouterState): Task {
} }
} }
function spawnTaskForMissingData(routerState: FlightRouterState): Task {
// Create a task for a new subtree that wasn't prefetched by the server.
// This shouldn't really ever happen but it's here just in case the Seed Data
// Tree and the Router State Tree disagree unexpectedly.
const pendingCacheNode = createPendingCacheNode(routerState, null, null)
return {
route: routerState,
node: pendingCacheNode,
children: null,
}
}
// Writes a dynamic server response into the tree created by // Writes a dynamic server response into the tree created by
// updateCacheNodeOnNavigation. All pending promises that were spawned by the // updateCacheNodeOnNavigation. All pending promises that were spawned by the
// navigation will be resolved, either with dynamic data from the server, or // navigation will be resolved, either with dynamic data from the server, or
...@@ -491,7 +472,7 @@ function finishTaskUsingDynamicDataPayload( ...@@ -491,7 +472,7 @@ function finishTaskUsingDynamicDataPayload(
function createPendingCacheNode( function createPendingCacheNode(
routerState: FlightRouterState, routerState: FlightRouterState,
prefetchData: CacheNodeSeedData | null, prefetchData: CacheNodeSeedData | null,
prefetchHead: React.ReactNode prefetchHead: React.ReactNode | null
): ReadyCacheNode { ): ReadyCacheNode {
const routerStateChildren = routerState[1] const routerStateChildren = routerState[1]
const prefetchDataChildren = prefetchData !== null ? prefetchData[2] : null const prefetchDataChildren = prefetchData !== null ? prefetchData[2] : null
......
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