Unverified Commit 20f72b93 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

[Segment Cache] Implement hash-only navigations (#76179)

When a navigation happens and the only thing that changes is the hash
fragment, we must do some special handling to ensure that the hash is
correctly scrolled into view.

Nearly all of this is handled by ScrollAndFocusHandler; for the Segment
Cache, the only thing we need to do is update a few of the fields on the
router state object, like `scrollableSegments` and `onlyHashChange`.
parent c929afc8
......@@ -101,6 +101,7 @@ function triggerLazyFetchForLeafSegments(
}
function handleNavigationResult(
url: URL,
state: ReadonlyReducerState,
mutable: Mutable,
pendingPush: boolean,
......@@ -112,9 +113,32 @@ function handleNavigationResult(
const newUrl = result.data
return handleExternalUrl(state, mutable, newUrl, pendingPush)
}
case NavigationResultTag.NoOp:
// The server responded with no change to the current page.
case NavigationResultTag.NoOp: {
// The server responded with no change to the current page. However, if
// the URL changed, we still need to update that.
const newCanonicalUrl = result.data.canonicalUrl
mutable.canonicalUrl = newCanonicalUrl
// Check if the only thing that changed was the hash fragment.
const oldUrl = new URL(state.canonicalUrl, url)
const onlyHashChange =
// We don't need to compare the origins, because client-driven
// navigations are always same-origin.
url.pathname === oldUrl.pathname &&
url.search === oldUrl.search &&
url.hash !== oldUrl.hash
if (onlyHashChange) {
// The only updated part of the URL is the hash.
mutable.onlyHashChange = true
mutable.shouldScroll = result.data.shouldScroll
mutable.hashFragment = url.hash
// Setting this to an empty array triggers a scroll for all new and
// updated segments. See `ScrollAndFocusHandler` for more details.
mutable.scrollableSegments = []
}
return handleMutable(state, mutable)
}
case NavigationResultTag.Success: {
// Received a new result.
mutable.cache = result.data.cacheNode
......@@ -122,14 +146,13 @@ function handleNavigationResult(
mutable.canonicalUrl = result.data.canonicalUrl
mutable.scrollableSegments = result.data.scrollableSegments
mutable.shouldScroll = result.data.shouldScroll
// TODO: Not yet implemented
// mutable.hashFragment = hash
mutable.hashFragment = result.data.hash
return handleMutable(state, mutable)
}
case NavigationResultTag.Async: {
return result.data.then(
(asyncResult) =>
handleNavigationResult(state, mutable, pendingPush, asyncResult),
handleNavigationResult(url, state, mutable, pendingPush, asyncResult),
// If the navigation failed, return the current state.
// TODO: This matches the current behavior but we need to do something
// better here if the network fails.
......@@ -138,9 +161,10 @@ function handleNavigationResult(
}
)
}
default:
const _exhaustiveCheck: never = result
default: {
result satisfies never
return state
}
}
}
......@@ -189,7 +213,7 @@ export function navigateReducer(
state.nextUrl,
shouldScroll
)
return handleNavigationResult(state, mutable, pendingPush, result)
return handleNavigationResult(url, state, mutable, pendingPush, result)
}
const prefetchValues = getOrCreatePrefetchCacheEntry({
......
......@@ -39,7 +39,10 @@ type MPANavigationResult = {
type NoOpNavigationResult = {
tag: NavigationResultTag.NoOp
data: null
data: {
canonicalUrl: string
shouldScroll: boolean
}
}
type SuccessfulNavigationResult = {
......@@ -50,6 +53,7 @@ type SuccessfulNavigationResult = {
canonicalUrl: string
scrollableSegments: Array<FlightSegmentPath>
shouldScroll: boolean
hash: string
}
}
......@@ -66,11 +70,6 @@ export type NavigationResult =
| NoOpNavigationResult
| AsyncNavigationResult
const noOpNavigationResult: NoOpNavigationResult = {
tag: NavigationResultTag.NoOp,
data: null,
}
/**
* Navigate to a new URL, using the Segment Cache to construct a response.
*
......@@ -97,7 +96,7 @@ export function navigate(
const prefetchSeedData = snapshot.seedData
const prefetchHead = route.head
const isPrefetchHeadPartial = route.isHeadPartial
const canonicalUrl = route.canonicalUrl
const newCanonicalUrl = route.canonicalUrl
return navigateUsingPrefetchedRouteTree(
url,
nextUrl,
......@@ -107,8 +106,9 @@ export function navigate(
prefetchSeedData,
prefetchHead,
isPrefetchHeadPartial,
canonicalUrl,
shouldScroll
newCanonicalUrl,
shouldScroll,
url.hash
)
}
// There's no matching prefetch for this route in the cache.
......@@ -119,7 +119,8 @@ export function navigate(
nextUrl,
currentCacheNode,
currentFlightRouterState,
shouldScroll
shouldScroll,
url.hash
),
}
}
......@@ -134,7 +135,8 @@ function navigateUsingPrefetchedRouteTree(
prefetchHead: HeadData | null,
isPrefetchHeadPartial: boolean,
canonicalUrl: string,
shouldScroll: boolean
shouldScroll: boolean,
hash: string
): 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
......@@ -169,11 +171,19 @@ function navigateUsingPrefetchedRouteTree(
currentCacheNode,
canonicalUrl,
scrollableSegments,
shouldScroll
shouldScroll,
hash
)
}
// The server sent back an empty tree patch. There's nothing to update.
return noOpNavigationResult
// The server sent back an empty tree patch. There's nothing to update, except
// possibly the URL.
return {
tag: NavigationResultTag.NoOp,
data: {
canonicalUrl,
shouldScroll,
},
}
}
function navigationTaskToResult(
......@@ -181,7 +191,8 @@ function navigationTaskToResult(
currentCacheNode: CacheNode,
canonicalUrl: string,
scrollableSegments: Array<FlightSegmentPath>,
shouldScroll: boolean
shouldScroll: boolean,
hash: string
): SuccessfulNavigationResult | MPANavigationResult {
const flightRouterState = task.route
if (flightRouterState === null) {
......@@ -201,6 +212,7 @@ function navigationTaskToResult(
canonicalUrl,
scrollableSegments,
shouldScroll,
hash,
},
}
}
......@@ -279,7 +291,8 @@ async function navigateDynamicallyWithNoPrefetch(
nextUrl: string | null,
currentCacheNode: CacheNode,
currentFlightRouterState: FlightRouterState,
shouldScroll: boolean
shouldScroll: boolean,
hash: string
): Promise<
MPANavigationResult | SuccessfulNavigationResult | NoOpNavigationResult
> {
......@@ -302,9 +315,6 @@ async function navigateDynamicallyWithNoPrefetch(
const { flightData, canonicalUrl: canonicalUrlOverride } =
await promiseForDynamicServerResponse
// TODO: Detect if the only thing that changed was the hash, like we do in
// in navigateReducer
if (typeof flightData === 'string') {
// This is an MPA navigation.
const newUrl = flightData
......@@ -364,11 +374,19 @@ async function navigateDynamicallyWithNoPrefetch(
currentCacheNode,
canonicalUrl,
scrollableSegments,
shouldScroll
shouldScroll,
hash
)
}
// The server sent back an empty tree patch. There's nothing to update.
return noOpNavigationResult
// The server sent back an empty tree patch. There's nothing to update, except
// possibly the URL.
return {
tag: NavigationResultTag.NoOp,
data: {
canonicalUrl,
shouldScroll,
},
}
}
function simulatePrefetchTreeUsingDynamicTreePatch(
......
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