@@ -167,18 +167,17 @@ function getScrollToTopElements(
167167
168168export function setupScrollRestoration ( router : AnyRouter , force ?: boolean ) {
169169 // Keep hash/top scrolling active even when sessionStorage is unavailable.
170+ const shouldSetupScrollRestoration = force ?? router . options . scrollRestoration
171+ const scroll = router . _scroll
170172
171- if ( force ?? router . options . scrollRestoration ) {
172- router . isScrollRestoring = true
173+ if ( shouldSetupScrollRestoration ) {
174+ scroll . restoring = true
173175 }
174176
175- if ( ( isServer ?? router . isServer ) || router . isScrollRestorationSetup ) {
177+ if ( isServer ?? router . isServer ) {
176178 return
177179 }
178180
179- router . isScrollRestorationSetup = true
180- ignoreScroll = false
181-
182181 const getKey =
183182 router . options . getScrollRestorationKey || defaultGetScrollRestorationKey
184183 const trackedScrollEntries = new Map < ScrollTarget , ScrollRestorationEntry > ( )
@@ -194,10 +193,8 @@ export function setupScrollRestoration(router: AnyRouter, force?: boolean) {
194193 trackedScrollEntries . set ( target , entry )
195194 }
196195
197- history . scrollRestoration = 'manual'
198-
199196 const onScroll = ( event : Event ) => {
200- if ( ignoreScroll || ! router . isScrollRestoring ) {
197+ if ( ignoreScroll || ! scroll . restoring ) {
201198 return
202199 }
203200
@@ -211,7 +208,7 @@ export function setupScrollRestoration(router: AnyRouter, force?: boolean) {
211208
212209 // Snapshot the current page's tracked scroll targets before navigation or unload.
213210 const snapshotCurrentScrollTargets = ( restoreKey : string ) => {
214- if ( ! router . isScrollRestoring ) {
211+ if ( ! scroll . restoring ) {
215212 return
216213 }
217214
@@ -227,32 +224,45 @@ export function setupScrollRestoration(router: AnyRouter, force?: boolean) {
227224 }
228225 }
229226
230- document . addEventListener ( 'scroll' , onScroll , true )
231- router . subscribe ( 'onBeforeLoad' , ( event ) => {
232- if ( event . fromLocation ) {
233- snapshotCurrentScrollTargets ( getKey ( event . fromLocation ) )
234- }
235- trackedScrollEntries . clear ( )
236- } )
237- addEventListener ( 'pagehide' , ( ) => {
238- snapshotCurrentScrollTargets (
239- getKey (
240- router . stores . resolvedLocation . get ( ) ?? router . stores . location . get ( ) ,
241- ) ,
242- )
243- persistScrollRestorationCache ( )
244- } )
227+ if ( shouldSetupScrollRestoration && ! scroll . restoration ) {
228+ scroll . restoration = true
229+ ignoreScroll = false
230+
231+ history . scrollRestoration = 'manual'
232+
233+ document . addEventListener ( 'scroll' , onScroll , true )
234+ router . subscribe ( 'onBeforeLoad' , ( event ) => {
235+ if ( event . fromLocation ) {
236+ snapshotCurrentScrollTargets ( getKey ( event . fromLocation ) )
237+ }
238+ trackedScrollEntries . clear ( )
239+ } )
240+ addEventListener ( 'pagehide' , ( ) => {
241+ snapshotCurrentScrollTargets (
242+ getKey (
243+ router . stores . resolvedLocation . get ( ) ?? router . stores . location . get ( ) ,
244+ ) ,
245+ )
246+ persistScrollRestorationCache ( )
247+ } )
248+ }
249+
250+ if ( scroll . reset ) {
251+ return
252+ }
253+
254+ scroll . reset = true
245255
246256 // Restore destination scroll after the new route has rendered.
247257 router . subscribe ( 'onRendered' , ( event ) => {
248258 const behavior = router . options . scrollRestorationBehavior
249259 const scrollToTopSelectors = router . options . scrollToTopSelectors
250- const shouldResetScroll = router . resetNextScroll
260+ const shouldResetScroll = scroll . next
251261 let scrollToTopElements : Array < Element > | undefined
252262 trackedScrollEntries . clear ( )
253263
254264 if ( ! shouldResetScroll ) {
255- router . resetNextScroll = true
265+ scroll . next = true
256266 }
257267
258268 if (
@@ -265,7 +275,7 @@ export function setupScrollRestoration(router: AnyRouter, force?: boolean) {
265275 const cacheKey = getKey ( event . toLocation )
266276 const fromCacheKey = event . fromLocation && getKey ( event . fromLocation )
267277
268- if ( router . isScrollRestoring && fromCacheKey && fromCacheKey !== cacheKey ) {
278+ if ( scroll . restoring && fromCacheKey && fromCacheKey !== cacheKey ) {
269279 const fromElementEntries = scrollRestorationCache [ fromCacheKey ]
270280
271281 if ( fromElementEntries ) {
@@ -317,7 +327,7 @@ export function setupScrollRestoration(router: AnyRouter, force?: boolean) {
317327 hashScrollIntoViewOptions &&
318328 ( action === 'PUSH' || action === 'REPLACE' )
319329
320- const elementEntries = router . isScrollRestoring
330+ const elementEntries = scroll . restoring
321331 ? scrollRestorationCache [ cacheKey ]
322332 : undefined
323333
0 commit comments