Unverified Commit e701b375 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

refactor: handle onlyHashChange logic sooner (#70569)

When only the hash fragment changes there's no reason to read anything
from the server. The `onlyHashChange` logic was spilling out into places
that shouldn't really care about it (like ppr-navigations) when really
we just want to update some state flags and exit early.

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 9ea0ff99
...@@ -67,8 +67,7 @@ export function updateCacheNodeOnNavigation( ...@@ -67,8 +67,7 @@ export function updateCacheNodeOnNavigation(
oldRouterState: FlightRouterState, oldRouterState: FlightRouterState,
newRouterState: FlightRouterState, newRouterState: FlightRouterState,
prefetchData: CacheNodeSeedData, prefetchData: CacheNodeSeedData,
prefetchHead: React.ReactNode, prefetchHead: React.ReactNode
onlyHashChange: boolean
): 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]
...@@ -137,12 +136,7 @@ export function updateCacheNodeOnNavigation( ...@@ -137,12 +136,7 @@ export function updateCacheNodeOnNavigation(
: undefined : undefined
let taskChild: Task | null let taskChild: Task | null
if (onlyHashChange && oldRouterStateChild !== undefined) { if (isPageSegment) {
// If only the hash fragment changed, we can re-use the existing cache.
// We spawn a "task" just to keep track of the updated router state; unlike most, it's
// already fulfilled and won't be affected by the dynamic response.
taskChild = spawnReusedTask(oldRouterStateChild)
} else if (isPageSegment) {
// This is a leaf segment — a page, not a shared layout. We always apply // This is a leaf segment — a page, not a shared layout. We always apply
// its data. // its data.
taskChild = spawnPendingTask( taskChild = spawnPendingTask(
...@@ -187,8 +181,7 @@ export function updateCacheNodeOnNavigation( ...@@ -187,8 +181,7 @@ export function updateCacheNodeOnNavigation(
oldRouterStateChild, oldRouterStateChild,
newRouterStateChild, newRouterStateChild,
prefetchDataChild, prefetchDataChild,
prefetchHead, prefetchHead
onlyHashChange
) )
} else { } else {
// The server didn't send any prefetch data for this segment. This // The server didn't send any prefetch data for this segment. This
......
...@@ -151,6 +151,26 @@ export function navigateReducer( ...@@ -151,6 +151,26 @@ export function navigateReducer(
return handleExternalUrl(state, mutable, flightData, pendingPush) return handleExternalUrl(state, mutable, flightData, pendingPush)
} }
const updatedCanonicalUrl = canonicalUrlOverride
? createHrefFromUrl(canonicalUrlOverride)
: href
const onlyHashChange =
!!hash &&
state.canonicalUrl.split('#', 1)[0] ===
updatedCanonicalUrl.split('#', 1)[0]
// If only the hash has changed, the server hasn't sent us any new data. We can just update
// the mutable properties responsible for URL and scroll handling and return early.
if (onlyHashChange) {
mutable.onlyHashChange = true
mutable.canonicalUrl = updatedCanonicalUrl
mutable.shouldScroll = shouldScroll
mutable.hashFragment = hash
mutable.scrollableSegments = []
return handleMutable(state, mutable)
}
if (prefetchValues.aliased) { if (prefetchValues.aliased) {
const result = handleAliasedPrefetchEntry( const result = handleAliasedPrefetchEntry(
state, state,
...@@ -169,16 +189,6 @@ export function navigateReducer( ...@@ -169,16 +189,6 @@ export function navigateReducer(
return result return result
} }
const updatedCanonicalUrl = canonicalUrlOverride
? createHrefFromUrl(canonicalUrlOverride)
: href
// Track if the navigation was only an update to the hash fragment
mutable.onlyHashChange =
!!hash &&
state.canonicalUrl.split('#', 1)[0] ===
updatedCanonicalUrl.split('#', 1)[0]
let currentTree = state.tree let currentTree = state.tree
let currentCache = state.cache let currentCache = state.cache
let scrollableSegments: FlightSegmentPath[] = [] let scrollableSegments: FlightSegmentPath[] = []
...@@ -237,8 +247,7 @@ export function navigateReducer( ...@@ -237,8 +247,7 @@ export function navigateReducer(
currentTree, currentTree,
treePatch, treePatch,
seedData, seedData,
head, head
mutable.onlyHashChange
) )
if (task !== null) { if (task !== null) {
...@@ -305,7 +314,6 @@ export function navigateReducer( ...@@ -305,7 +314,6 @@ export function navigateReducer(
if ( if (
prefetchValues.status === PrefetchCacheEntryStatus.stale && prefetchValues.status === PrefetchCacheEntryStatus.stale &&
!mutable.onlyHashChange &&
!isFirstRead !isFirstRead
) { ) {
// When we have a stale prefetch entry, we only want to re-use the loading state of the route we're navigating to, to support instant loading navigations // When we have a stale prefetch entry, we only want to re-use the loading state of the route we're navigating to, to support instant loading navigations
......
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