fix(nuxt): prevent initial scroll jump when loading page (#25483)

This commit is contained in:
Daniel Roe 2024-01-29 13:56:23 +00:00 committed by GitHub
parent 995acd9983
commit ecc4c8e0c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -78,13 +78,25 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
startPosition = savedPosition
return
}
// reset scroll behavior to initial value
router.options.scrollBehavior = routerOptions.scrollBehavior
return routerOptions.scrollBehavior?.(to, START_LOCATION, startPosition || savedPosition)
if (routerOptions.scrollBehavior) {
// reset scroll behavior to initial value
router.options.scrollBehavior = routerOptions.scrollBehavior
if ('scrollRestoration' in window.history) {
const unsub = router.beforeEach(() => {
unsub()
window.history.scrollRestoration = 'manual'
})
}
return routerOptions.scrollBehavior(to, START_LOCATION, startPosition || savedPosition)
}
},
history,
routes
})
if (import.meta.client && 'scrollRestoration' in window.history) {
window.history.scrollRestoration = 'auto'
}
nuxtApp.vueApp.use(router)
const previousRoute = shallowRef(router.currentRoute.value)