@@ -2444,7 +2444,7 @@ export function createRouter(init: RouterInit): Router {
24442444 ) {
24452445 savedScrollPositions = positions ;
24462446 getScrollPosition = getPosition ;
2447- getScrollRestorationKey = getKey || ( ( location ) => location . key ) ;
2447+ getScrollRestorationKey = getKey || null ;
24482448
24492449 // Perform initial hydration scroll restoration, since we miss the boat on
24502450 // the initial updateState() because we've not yet rendered <ScrollRestoration/>
@@ -2464,15 +2464,23 @@ export function createRouter(init: RouterInit): Router {
24642464 } ;
24652465 }
24662466
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+
24672478 function saveScrollPosition (
24682479 location : Location ,
24692480 matches : AgnosticDataRouteMatch [ ]
24702481 ) : 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 ) ;
24762484 savedScrollPositions [ key ] = getScrollPosition ( ) ;
24772485 }
24782486 }
@@ -2481,11 +2489,8 @@ export function createRouter(init: RouterInit): Router {
24812489 location : Location ,
24822490 matches : AgnosticDataRouteMatch [ ]
24832491 ) : 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 ) ;
24892494 let y = savedScrollPositions [ key ] ;
24902495 if ( typeof y === "number" ) {
24912496 return y ;
0 commit comments