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( ...@@ -101,6 +101,7 @@ function triggerLazyFetchForLeafSegments(
} }
function handleNavigationResult( function handleNavigationResult(
url: URL,
state: ReadonlyReducerState, state: ReadonlyReducerState,
mutable: Mutable, mutable: Mutable,
pendingPush: boolean, pendingPush: boolean,
...@@ -112,9 +113,32 @@ function handleNavigationResult( ...@@ -112,9 +113,32 @@ function handleNavigationResult(
const newUrl = result.data const newUrl = result.data
return handleExternalUrl(state, mutable, newUrl, pendingPush) return handleExternalUrl(state, mutable, newUrl, pendingPush)
} }
case NavigationResultTag.NoOp: case NavigationResultTag.NoOp: {
// The server responded with no change to the current page. // 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) return handleMutable(state, mutable)
}
case NavigationResultTag.Success: { case NavigationResultTag.Success: {
// Received a new result. // Received a new result.
mutable.cache = result.data.cacheNode mutable.cache = result.data.cacheNode
...@@ -122,14 +146,13 @@ function handleNavigationResult( ...@@ -122,14 +146,13 @@ function handleNavigationResult(
mutable.canonicalUrl = result.data.canonicalUrl mutable.canonicalUrl = result.data.canonicalUrl
mutable.scrollableSegments = result.data.scrollableSegments mutable.scrollableSegments = result.data.scrollableSegments
mutable.shouldScroll = result.data.shouldScroll mutable.shouldScroll = result.data.shouldScroll
// TODO: Not yet implemented mutable.hashFragment = result.data.hash
// mutable.hashFragment = hash
return handleMutable(state, mutable) return handleMutable(state, mutable)
} }
case NavigationResultTag.Async: { case NavigationResultTag.Async: {
return result.data.then( return result.data.then(
(asyncResult) => (asyncResult) =>
handleNavigationResult(state, mutable, pendingPush, asyncResult), handleNavigationResult(url, state, mutable, pendingPush, asyncResult),
// If the navigation failed, return the current state. // If the navigation failed, return the current state.
// TODO: This matches the current behavior but we need to do something // TODO: This matches the current behavior but we need to do something
// better here if the network fails. // better here if the network fails.
...@@ -138,9 +161,10 @@ function handleNavigationResult( ...@@ -138,9 +161,10 @@ function handleNavigationResult(
} }
) )
} }
default: default: {
const _exhaustiveCheck: never = result result satisfies never
return state return state
}
} }
} }
...@@ -189,7 +213,7 @@ export function navigateReducer( ...@@ -189,7 +213,7 @@ export function navigateReducer(
state.nextUrl, state.nextUrl,
shouldScroll shouldScroll
) )
return handleNavigationResult(state, mutable, pendingPush, result) return handleNavigationResult(url, state, mutable, pendingPush, result)
} }
const prefetchValues = getOrCreatePrefetchCacheEntry({ const prefetchValues = getOrCreatePrefetchCacheEntry({
......
...@@ -39,7 +39,10 @@ type MPANavigationResult = { ...@@ -39,7 +39,10 @@ type MPANavigationResult = {
type NoOpNavigationResult = { type NoOpNavigationResult = {
tag: NavigationResultTag.NoOp tag: NavigationResultTag.NoOp
data: null data: {
canonicalUrl: string
shouldScroll: boolean
}
} }
type SuccessfulNavigationResult = { type SuccessfulNavigationResult = {
...@@ -50,6 +53,7 @@ type SuccessfulNavigationResult = { ...@@ -50,6 +53,7 @@ type SuccessfulNavigationResult = {
canonicalUrl: string canonicalUrl: string
scrollableSegments: Array<FlightSegmentPath> scrollableSegments: Array<FlightSegmentPath>
shouldScroll: boolean shouldScroll: boolean
hash: string
} }
} }
...@@ -66,11 +70,6 @@ export type NavigationResult = ...@@ -66,11 +70,6 @@ export type NavigationResult =
| NoOpNavigationResult | NoOpNavigationResult
| AsyncNavigationResult | AsyncNavigationResult
const noOpNavigationResult: NoOpNavigationResult = {
tag: NavigationResultTag.NoOp,
data: null,
}
/** /**
* Navigate to a new URL, using the Segment Cache to construct a response. * Navigate to a new URL, using the Segment Cache to construct a response.
* *
...@@ -97,7 +96,7 @@ export function navigate( ...@@ -97,7 +96,7 @@ export function navigate(
const prefetchSeedData = snapshot.seedData const prefetchSeedData = snapshot.seedData
const prefetchHead = route.head const prefetchHead = route.head
const isPrefetchHeadPartial = route.isHeadPartial const isPrefetchHeadPartial = route.isHeadPartial
const canonicalUrl = route.canonicalUrl const newCanonicalUrl = route.canonicalUrl
return navigateUsingPrefetchedRouteTree( return navigateUsingPrefetchedRouteTree(
url, url,
nextUrl, nextUrl,
...@@ -107,8 +106,9 @@ export function navigate( ...@@ -107,8 +106,9 @@ export function navigate(
prefetchSeedData, prefetchSeedData,
prefetchHead, prefetchHead,
isPrefetchHeadPartial, isPrefetchHeadPartial,
canonicalUrl, newCanonicalUrl,
shouldScroll shouldScroll,
url.hash
) )
} }
// There's no matching prefetch for this route in the cache. // There's no matching prefetch for this route in the cache.
...@@ -119,7 +119,8 @@ export function navigate( ...@@ -119,7 +119,8 @@ export function navigate(
nextUrl, nextUrl,
currentCacheNode, currentCacheNode,
currentFlightRouterState, currentFlightRouterState,
shouldScroll shouldScroll,
url.hash
), ),
} }
} }
...@@ -134,7 +135,8 @@ function navigateUsingPrefetchedRouteTree( ...@@ -134,7 +135,8 @@ function navigateUsingPrefetchedRouteTree(
prefetchHead: HeadData | null, prefetchHead: HeadData | null,
isPrefetchHeadPartial: boolean, isPrefetchHeadPartial: boolean,
canonicalUrl: string, canonicalUrl: string,
shouldScroll: boolean shouldScroll: boolean,
hash: string
): 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
...@@ -169,11 +171,19 @@ function navigateUsingPrefetchedRouteTree( ...@@ -169,11 +171,19 @@ function navigateUsingPrefetchedRouteTree(
currentCacheNode, currentCacheNode,
canonicalUrl, canonicalUrl,
scrollableSegments, scrollableSegments,
shouldScroll shouldScroll,
hash
) )
} }
// 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, except
return noOpNavigationResult // possibly the URL.
return {
tag: NavigationResultTag.NoOp,
data: {
canonicalUrl,
shouldScroll,
},
}
} }
function navigationTaskToResult( function navigationTaskToResult(
...@@ -181,7 +191,8 @@ function navigationTaskToResult( ...@@ -181,7 +191,8 @@ function navigationTaskToResult(
currentCacheNode: CacheNode, currentCacheNode: CacheNode,
canonicalUrl: string, canonicalUrl: string,
scrollableSegments: Array<FlightSegmentPath>, scrollableSegments: Array<FlightSegmentPath>,
shouldScroll: boolean shouldScroll: boolean,
hash: string
): SuccessfulNavigationResult | MPANavigationResult { ): SuccessfulNavigationResult | MPANavigationResult {
const flightRouterState = task.route const flightRouterState = task.route
if (flightRouterState === null) { if (flightRouterState === null) {
...@@ -201,6 +212,7 @@ function navigationTaskToResult( ...@@ -201,6 +212,7 @@ function navigationTaskToResult(
canonicalUrl, canonicalUrl,
scrollableSegments, scrollableSegments,
shouldScroll, shouldScroll,
hash,
}, },
} }
} }
...@@ -279,7 +291,8 @@ async function navigateDynamicallyWithNoPrefetch( ...@@ -279,7 +291,8 @@ async function navigateDynamicallyWithNoPrefetch(
nextUrl: string | null, nextUrl: string | null,
currentCacheNode: CacheNode, currentCacheNode: CacheNode,
currentFlightRouterState: FlightRouterState, currentFlightRouterState: FlightRouterState,
shouldScroll: boolean shouldScroll: boolean,
hash: string
): Promise< ): Promise<
MPANavigationResult | SuccessfulNavigationResult | NoOpNavigationResult MPANavigationResult | SuccessfulNavigationResult | NoOpNavigationResult
> { > {
...@@ -302,9 +315,6 @@ async function navigateDynamicallyWithNoPrefetch( ...@@ -302,9 +315,6 @@ async function navigateDynamicallyWithNoPrefetch(
const { flightData, canonicalUrl: canonicalUrlOverride } = const { flightData, canonicalUrl: canonicalUrlOverride } =
await promiseForDynamicServerResponse await promiseForDynamicServerResponse
// TODO: Detect if the only thing that changed was the hash, like we do in
// in navigateReducer
if (typeof flightData === 'string') { if (typeof flightData === 'string') {
// This is an MPA navigation. // This is an MPA navigation.
const newUrl = flightData const newUrl = flightData
...@@ -364,11 +374,19 @@ async function navigateDynamicallyWithNoPrefetch( ...@@ -364,11 +374,19 @@ async function navigateDynamicallyWithNoPrefetch(
currentCacheNode, currentCacheNode,
canonicalUrl, canonicalUrl,
scrollableSegments, scrollableSegments,
shouldScroll shouldScroll,
hash
) )
} }
// 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, except
return noOpNavigationResult // possibly the URL.
return {
tag: NavigationResultTag.NoOp,
data: {
canonicalUrl,
shouldScroll,
},
}
} }
function simulatePrefetchTreeUsingDynamicTreePatch( 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