@@ -2444,7 +2444,7 @@ export function createRouter(init: RouterInit): Router {
2444
2444
) {
2445
2445
savedScrollPositions = positions ;
2446
2446
getScrollPosition = getPosition ;
2447
- getScrollRestorationKey = getKey || ( ( location ) => location . key ) ;
2447
+ getScrollRestorationKey = getKey || null ;
2448
2448
2449
2449
// Perform initial hydration scroll restoration, since we miss the boat on
2450
2450
// the initial updateState() because we've not yet rendered <ScrollRestoration/>
@@ -2464,15 +2464,23 @@ export function createRouter(init: RouterInit): Router {
2464
2464
} ;
2465
2465
}
2466
2466
2467
+ function getScrollKey ( location : Location , matches : AgnosticDataRouteMatch [ ] ) {
2468
+ if ( getScrollRestorationKey ) {
2469
+ let key = getScrollRestorationKey (
2470
+ location ,
2471
+ matches . map ( ( m ) => createUseMatchesMatch ( m , state . loaderData ) )
2472
+ ) ;
2473
+ return key || location . key ;
2474
+ }
2475
+ return location . key ;
2476
+ }
2477
+
2467
2478
function saveScrollPosition (
2468
2479
location : Location ,
2469
2480
matches : AgnosticDataRouteMatch [ ]
2470
2481
) : void {
2471
- if ( savedScrollPositions && getScrollRestorationKey && getScrollPosition ) {
2472
- let userMatches = matches . map ( ( m ) =>
2473
- createUseMatchesMatch ( m , state . loaderData )
2474
- ) ;
2475
- let key = getScrollRestorationKey ( location , userMatches ) || location . key ;
2482
+ if ( savedScrollPositions && getScrollPosition ) {
2483
+ let key = getScrollKey ( location , matches ) ;
2476
2484
savedScrollPositions [ key ] = getScrollPosition ( ) ;
2477
2485
}
2478
2486
}
@@ -2481,11 +2489,8 @@ export function createRouter(init: RouterInit): Router {
2481
2489
location : Location ,
2482
2490
matches : AgnosticDataRouteMatch [ ]
2483
2491
) : number | null {
2484
- if ( savedScrollPositions && getScrollRestorationKey && getScrollPosition ) {
2485
- let userMatches = matches . map ( ( m ) =>
2486
- createUseMatchesMatch ( m , state . loaderData )
2487
- ) ;
2488
- let key = getScrollRestorationKey ( location , userMatches ) || location . key ;
2492
+ if ( savedScrollPositions ) {
2493
+ let key = getScrollKey ( location , matches ) ;
2489
2494
let y = savedScrollPositions [ key ] ;
2490
2495
if ( typeof y === "number" ) {
2491
2496
return y ;
0 commit comments