@@ -108,31 +108,31 @@ export function getScrollRestorationScript(
108108 return `(${ minifiedScrollRestorationScript } )(${ escapeHtml ( JSON . stringify ( options ) ) } )`
109109}
110110
111- function shouldRestoreScrollForLocation (
112- router : AnyRouter ,
113- location : ParsedLocation ,
114- ) {
115- if ( typeof router . options . scrollRestoration === 'function' ) {
116- return router . options . scrollRestoration ( { location } )
117- }
118-
119- return true
120- }
121-
122111export function getScrollRestorationScriptForRouter ( router : AnyRouter ) {
123- if ( ! shouldRestoreScrollForLocation ( router , router . latestLocation ) ) {
112+ if (
113+ typeof router . options . scrollRestoration === 'function' &&
114+ ! router . options . scrollRestoration ( { location : router . latestLocation } )
115+ ) {
124116 return null
125117 }
126118
127- const getKey =
128- router . options . getScrollRestorationKey || defaultGetScrollRestorationKey
129- const userKey = getKey ( router . latestLocation )
130- const defaultKey = defaultGetScrollRestorationKey ( router . latestLocation )
119+ const getKey = router . options . getScrollRestorationKey
120+ if ( ! getKey ) {
121+ return defaultInlineScrollRestorationScript
122+ }
123+
124+ const location = router . latestLocation
125+ const userKey = getKey ( location )
126+ const defaultKey = defaultGetScrollRestorationKey ( location )
127+
128+ if ( userKey === defaultKey ) {
129+ return defaultInlineScrollRestorationScript
130+ }
131131
132132 return getScrollRestorationScript ( {
133133 storageKey,
134134 shouldScrollRestoration : true ,
135- key : userKey !== defaultKey ? userKey : undefined ,
135+ key : userKey ,
136136 } )
137137}
138138
@@ -174,23 +174,22 @@ export function getElementScrollRestorationEntry(
174174 } ,
175175) : ScrollRestorationEntry | undefined {
176176 const getKey = options . getKey || defaultGetScrollRestorationKey
177-
178- let elementSelector = ''
177+ const restoreKey = getKey ( router . latestLocation )
179178
180179 if ( options . id ) {
181- elementSelector = `[${ scrollRestorationIdAttribute } ="${ options . id } "]`
182- } else {
183- const element = options . getElement ?.( )
184- if ( ! element ) {
185- return
186- }
187- elementSelector =
188- element instanceof Window ? windowScrollTarget : getCssSelector ( element )
180+ return scrollRestorationCache ?. state [ restoreKey ] ?. [
181+ `[${ scrollRestorationIdAttribute } ="${ options . id } "]`
182+ ]
189183 }
190184
191- const restoreKey = getKey ( router . latestLocation )
192- const byKey = scrollRestorationCache ?. state [ restoreKey ]
193- return byKey ?. [ elementSelector ]
185+ const element = options . getElement ?.( )
186+ if ( ! element ) {
187+ return
188+ }
189+
190+ return scrollRestorationCache ?. state [ restoreKey ] ?. [
191+ element instanceof Window ? windowScrollTarget : getCssSelector ( element )
192+ ]
194193}
195194
196195let ignoreScroll = false
@@ -310,7 +309,10 @@ export function setupScrollRestoration(router: AnyRouter, force?: boolean) {
310309 return
311310 }
312311
313- if ( ! shouldRestoreScrollForLocation ( router , router . latestLocation ) ) {
312+ if (
313+ typeof router . options . scrollRestoration === 'function' &&
314+ ! router . options . scrollRestoration ( { location : router . latestLocation } )
315+ ) {
314316 return
315317 }
316318
0 commit comments