Skip to content

Commit 42142c2

Browse files
committed
fix(router): scroll to top on cache-miss popstate
Companion to the previous commit. Setting history.scrollRestoration = 'manual' takes the browser out of the scroll-restoration game on back/forward — which is great when we have a cached snapshot (we restore exactly where the user left off) but leaves the cache-miss case unhandled. Without an explicit scrollTo, the cache-miss popstate would leave scroll wherever the user was on the page they popped FROM, which is jarring. Adds an explicit window.scrollTo(0, 0) in the cache-miss branch of performNavigation's popstate path — fetchAndApply itself skips its scroll block when recordHistory=false (which is always the case for popstate), so this is the only place to put it. This matches what browser-native scroll restoration would do for a history entry it has no saved scroll for. The cached-hit branch is unchanged.
1 parent 5a3b684 commit 42142c2

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

packages/core/src/router-client.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,13 @@ async function performNavigation(href, isPopState, frameId) {
609609
return;
610610
}
611611
}
612+
// Cache-miss popstate. Browser-native scroll restoration is
613+
// disabled (we set scrollRestoration='manual') — so without
614+
// explicit handling, scroll would just stay where the user was
615+
// on the page they popped FROM. Scroll to top as the reasonable
616+
// default; fetchAndApply skips its own scroll handling when
617+
// recordHistory=false (which is the case here).
618+
if (typeof window !== 'undefined') window.scrollTo(0, 0);
612619
}
613620

614621
await fetchAndApply(href, frameId, !isPopState, optimisticState, 'GET', null, signal, myToken);

0 commit comments

Comments
 (0)