Unverified Commit 856e2b19 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

[Segment Cache] Implement scroll-to-new-page (#76170)

This implements Next's scroll-to-new-page behavior in the Segment Cache.

The LayoutRouter expects an array of segment paths that represents every
page in the new route tree. In the old implementation, this is generated
by the `generateSegmentsFromPatch` function. In the new implementation,
I've chosen to generate this array during the main tree traversal that
happens in `startPPRNavigation`.

As a future improvement, we should use the same segment path type that
we use to access Segment Cache entries; there's no reason to have two
representations of the same concept.
parent aeaa006b
...@@ -120,10 +120,10 @@ function handleNavigationResult( ...@@ -120,10 +120,10 @@ function handleNavigationResult(
mutable.cache = result.data.cacheNode mutable.cache = result.data.cacheNode
mutable.patchedTree = result.data.flightRouterState mutable.patchedTree = result.data.flightRouterState
mutable.canonicalUrl = result.data.canonicalUrl mutable.canonicalUrl = result.data.canonicalUrl
mutable.scrollableSegments = result.data.scrollableSegments
mutable.shouldScroll = result.data.shouldScroll
// TODO: Not yet implemented // TODO: Not yet implemented
// mutable.scrollableSegments = scrollableSegments
// mutable.hashFragment = hash // mutable.hashFragment = hash
// mutable.shouldScroll = shouldScroll
return handleMutable(state, mutable) return handleMutable(state, mutable)
} }
case NavigationResultTag.Async: { case NavigationResultTag.Async: {
...@@ -186,7 +186,8 @@ export function navigateReducer( ...@@ -186,7 +186,8 @@ export function navigateReducer(
url, url,
state.cache, state.cache,
state.tree, state.tree,
state.nextUrl state.nextUrl,
shouldScroll
) )
return handleNavigationResult(state, mutable, pendingPush, result) return handleNavigationResult(state, mutable, pendingPush, result)
} }
...@@ -311,7 +312,8 @@ export function navigateReducer( ...@@ -311,7 +312,8 @@ export function navigateReducer(
treePatch, treePatch,
seedData, seedData,
head, head,
isHeadPartial isHeadPartial,
scrollableSegments
) )
if (task !== null) { if (task !== null) {
...@@ -437,20 +439,23 @@ export function navigateReducer( ...@@ -437,20 +439,23 @@ export function navigateReducer(
// segments in the FlightDataPath will be able to reference the updated cache. // segments in the FlightDataPath will be able to reference the updated cache.
currentCache = cache currentCache = cache
} }
}
currentTree = newTree
for (const subSegment of generateSegmentsFromPatch(treePatch)) { for (const subSegment of generateSegmentsFromPatch(treePatch)) {
const scrollableSegmentPath = [...flightSegmentPath, ...subSegment] const scrollableSegmentPath = [
// Filter out the __DEFAULT__ paths as they shouldn't be scrolled to in this case. ...flightSegmentPath,
if ( ...subSegment,
scrollableSegmentPath[scrollableSegmentPath.length - 1] !== ]
DEFAULT_SEGMENT_KEY // Filter out the __DEFAULT__ paths as they shouldn't be scrolled to in this case.
) { if (
scrollableSegments.push(scrollableSegmentPath) scrollableSegmentPath[scrollableSegmentPath.length - 1] !==
DEFAULT_SEGMENT_KEY
) {
scrollableSegments.push(scrollableSegmentPath)
}
} }
} }
currentTree = newTree
} }
} }
......
...@@ -48,6 +48,8 @@ type SuccessfulNavigationResult = { ...@@ -48,6 +48,8 @@ type SuccessfulNavigationResult = {
flightRouterState: FlightRouterState flightRouterState: FlightRouterState
cacheNode: CacheNode cacheNode: CacheNode
canonicalUrl: string canonicalUrl: string
scrollableSegments: Array<FlightSegmentPath>
shouldScroll: boolean
} }
} }
...@@ -81,7 +83,8 @@ export function navigate( ...@@ -81,7 +83,8 @@ export function navigate(
url: URL, url: URL,
currentCacheNode: CacheNode, currentCacheNode: CacheNode,
currentFlightRouterState: FlightRouterState, currentFlightRouterState: FlightRouterState,
nextUrl: string | null nextUrl: string | null,
shouldScroll: boolean
): NavigationResult { ): NavigationResult {
const now = Date.now() const now = Date.now()
...@@ -104,7 +107,8 @@ export function navigate( ...@@ -104,7 +107,8 @@ export function navigate(
prefetchSeedData, prefetchSeedData,
prefetchHead, prefetchHead,
isPrefetchHeadPartial, isPrefetchHeadPartial,
canonicalUrl canonicalUrl,
shouldScroll
) )
} }
// There's no matching prefetch for this route in the cache. // There's no matching prefetch for this route in the cache.
...@@ -114,7 +118,8 @@ export function navigate( ...@@ -114,7 +118,8 @@ export function navigate(
url, url,
nextUrl, nextUrl,
currentCacheNode, currentCacheNode,
currentFlightRouterState currentFlightRouterState,
shouldScroll
), ),
} }
} }
...@@ -128,7 +133,8 @@ function navigateUsingPrefetchedRouteTree( ...@@ -128,7 +133,8 @@ function navigateUsingPrefetchedRouteTree(
prefetchSeedData: CacheNodeSeedData | null, prefetchSeedData: CacheNodeSeedData | null,
prefetchHead: HeadData | null, prefetchHead: HeadData | null,
isPrefetchHeadPartial: boolean, isPrefetchHeadPartial: boolean,
canonicalUrl: string canonicalUrl: string,
shouldScroll: boolean
): SuccessfulNavigationResult | NoOpNavigationResult | MPANavigationResult { ): SuccessfulNavigationResult | NoOpNavigationResult | MPANavigationResult {
// Recursively construct a prefetch tree by reading from the Segment Cache. To // Recursively construct a prefetch tree by reading from the Segment Cache. To
// maintain compatibility, we output the same data structures as the old // maintain compatibility, we output the same data structures as the old
...@@ -136,13 +142,15 @@ function navigateUsingPrefetchedRouteTree( ...@@ -136,13 +142,15 @@ function navigateUsingPrefetchedRouteTree(
// TODO: Eventually updateCacheNodeOnNavigation (or the equivalent) should // TODO: Eventually updateCacheNodeOnNavigation (or the equivalent) should
// read from the Segment Cache directly. It's only structured this way for now // read from the Segment Cache directly. It's only structured this way for now
// so we can share code with the old prefetching implementation. // so we can share code with the old prefetching implementation.
const scrollableSegments: Array<FlightSegmentPath> = []
const task = startPPRNavigation( const task = startPPRNavigation(
currentCacheNode, currentCacheNode,
currentFlightRouterState, currentFlightRouterState,
prefetchFlightRouterState, prefetchFlightRouterState,
prefetchSeedData, prefetchSeedData,
prefetchHead, prefetchHead,
isPrefetchHeadPartial isPrefetchHeadPartial,
scrollableSegments
) )
if (task !== null) { if (task !== null) {
const dynamicRequestTree = task.dynamicRequestTree const dynamicRequestTree = task.dynamicRequestTree
...@@ -156,7 +164,13 @@ function navigateUsingPrefetchedRouteTree( ...@@ -156,7 +164,13 @@ function navigateUsingPrefetchedRouteTree(
// The prefetched tree does not contain dynamic holes — it's // The prefetched tree does not contain dynamic holes — it's
// fully static. We can skip the dynamic request. // fully static. We can skip the dynamic request.
} }
return navigationTaskToResult(task, currentCacheNode, canonicalUrl) return navigationTaskToResult(
task,
currentCacheNode,
canonicalUrl,
scrollableSegments,
shouldScroll
)
} }
// The server sent back an empty tree patch. There's nothing to update. // The server sent back an empty tree patch. There's nothing to update.
return noOpNavigationResult return noOpNavigationResult
...@@ -165,7 +179,9 @@ function navigateUsingPrefetchedRouteTree( ...@@ -165,7 +179,9 @@ function navigateUsingPrefetchedRouteTree(
function navigationTaskToResult( function navigationTaskToResult(
task: PPRNavigationTask, task: PPRNavigationTask,
currentCacheNode: CacheNode, currentCacheNode: CacheNode,
canonicalUrl: string canonicalUrl: string,
scrollableSegments: Array<FlightSegmentPath>,
shouldScroll: boolean
): SuccessfulNavigationResult | MPANavigationResult { ): SuccessfulNavigationResult | MPANavigationResult {
const flightRouterState = task.route const flightRouterState = task.route
if (flightRouterState === null) { if (flightRouterState === null) {
...@@ -183,6 +199,8 @@ function navigationTaskToResult( ...@@ -183,6 +199,8 @@ function navigationTaskToResult(
flightRouterState, flightRouterState,
cacheNode: newCacheNode !== null ? newCacheNode : currentCacheNode, cacheNode: newCacheNode !== null ? newCacheNode : currentCacheNode,
canonicalUrl, canonicalUrl,
scrollableSegments,
shouldScroll,
}, },
} }
} }
...@@ -260,7 +278,8 @@ async function navigateDynamicallyWithNoPrefetch( ...@@ -260,7 +278,8 @@ async function navigateDynamicallyWithNoPrefetch(
url: URL, url: URL,
nextUrl: string | null, nextUrl: string | null,
currentCacheNode: CacheNode, currentCacheNode: CacheNode,
currentFlightRouterState: FlightRouterState currentFlightRouterState: FlightRouterState,
shouldScroll: boolean
): Promise< ): Promise<
MPANavigationResult | SuccessfulNavigationResult | NoOpNavigationResult MPANavigationResult | SuccessfulNavigationResult | NoOpNavigationResult
> { > {
...@@ -314,13 +333,15 @@ async function navigateDynamicallyWithNoPrefetch( ...@@ -314,13 +333,15 @@ async function navigateDynamicallyWithNoPrefetch(
) )
// Now we proceed exactly as we would for normal navigation. // Now we proceed exactly as we would for normal navigation.
const scrollableSegments: Array<FlightSegmentPath> = []
const task = startPPRNavigation( const task = startPPRNavigation(
currentCacheNode, currentCacheNode,
currentFlightRouterState, currentFlightRouterState,
prefetchFlightRouterState, prefetchFlightRouterState,
prefetchSeedData, prefetchSeedData,
prefetchHead, prefetchHead,
isPrefetchHeadPartial isPrefetchHeadPartial,
scrollableSegments
) )
if (task !== null) { if (task !== null) {
// In this case, we've already sent the dynamic request, so we don't // In this case, we've already sent the dynamic request, so we don't
...@@ -338,7 +359,13 @@ async function navigateDynamicallyWithNoPrefetch( ...@@ -338,7 +359,13 @@ async function navigateDynamicallyWithNoPrefetch(
// The prefetched tree does not contain dynamic holes — it's // The prefetched tree does not contain dynamic holes — it's
// fully static. We don't need to process the server response further. // fully static. We don't need to process the server response further.
} }
return navigationTaskToResult(task, currentCacheNode, canonicalUrl) return navigationTaskToResult(
task,
currentCacheNode,
canonicalUrl,
scrollableSegments,
shouldScroll
)
} }
// The server sent back an empty tree patch. There's nothing to update. // The server sent back an empty tree patch. There's nothing to update.
return noOpNavigationResult return noOpNavigationResult
......
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