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