@@ -141,7 +141,10 @@ export const setupBrowserIntegrationViaNavigation = ({
141141 // Aborting: each run aborts the previous one, and the navigate event's own
142142 // signal (superseded navigation, stop button) aborts the current one.
143143 let abortController = null ;
144- const runRouting = ( url , { reason, navigationType, state, abortEvent } ) => {
144+ const runRouting = (
145+ url ,
146+ { reason, navigationType, state, abortEvent, urlLeft } ,
147+ ) => {
145148 const redirectionUrl = resolveRouteRedirection ( url ) ;
146149 if ( redirectionUrl ) {
147150 redirectAway ( redirectionUrl ) ;
@@ -205,7 +208,7 @@ export const setupBrowserIntegrationViaNavigation = ({
205208 // Same rule, same timing as via_history.js's own, and the same reading of
206209 // a replace: only the row that travels knows one of its own is an arrival.
207210 if ( navigationType === "push" ) {
208- whenRenderingResumes ( ( ) => startAtTop ( url ) ) ;
211+ whenRenderingResumes ( ( ) => startAtTop ( url , { from : urlLeft } ) ) ;
209212 } else if ( navigationType === "traverse" ) {
210213 whenRenderingResumes ( ( ) => restoreScrollPosition ( url ) ) ;
211214 }
@@ -340,7 +343,10 @@ export const setupBrowserIntegrationViaNavigation = ({
340343 event . sourceElement || ( event . info ? event . info . element : undefined ) ,
341344 routeTransition : event . info ? event . info . routeTransition : undefined ,
342345 } ) ;
343- const isSameUrl = url === window . location . href ;
346+ // The url the reader is being taken away from, read here because the
347+ // commit — and with it the document url — happens before the handler runs.
348+ const urlLeft = window . location . href ;
349+ const isSameUrl = url === urlLeft ;
344350 event . intercept ( {
345351 // The browser would scroll at commit time — before the picture of the
346352 // page being left is taken. The shared scroll machinery does it at the
@@ -366,6 +372,7 @@ export const setupBrowserIntegrationViaNavigation = ({
366372 navigationType,
367373 state,
368374 abortEvent : event . signal ,
375+ urlLeft,
369376 } ) ;
370377 // The handler's promise IS the navigation for the browser (its
371378 // loading UI follows it) — but a routing that fails is displayed by
0 commit comments