fix(nuxt): use setTimeout before scrolling when navigating (#25817)

This commit is contained in:
Jonathan Walters 2024-02-22 06:09:51 -05:00 committed by GitHub
parent 6b504258bb
commit 0152cf48eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,4 @@
import type { RouteLocationNormalized, RouterScrollBehavior } from '#vue-router' import type { RouteLocationNormalized, RouterScrollBehavior } from '#vue-router'
import { nextTick } from 'vue'
import type { RouterConfig } from 'nuxt/schema' import type { RouterConfig } from 'nuxt/schema'
import { useNuxtApp } from '#app/nuxt' import { useNuxtApp } from '#app/nuxt'
import { isChangingPage } from '#app/components/utils' import { isChangingPage } from '#app/components/utils'
@ -36,6 +35,8 @@ export default <RouterConfig> {
if (to.hash) { if (to.hash) {
return { el: to.hash, top: _getHashElementScrollMarginTop(to.hash), behavior } return { el: to.hash, top: _getHashElementScrollMarginTop(to.hash), behavior }
} }
// The route isn't changing so keep current scroll position
return false
} }
// Wait for `page:transition:finish` or `page:finish` depending on if transitions are enabled or not // Wait for `page:transition:finish` or `page:finish` depending on if transitions are enabled or not
@ -43,7 +44,7 @@ export default <RouterConfig> {
const hookToWait = (hasTransition(from) && hasTransition(to)) ? 'page:transition:finish' : 'page:finish' const hookToWait = (hasTransition(from) && hasTransition(to)) ? 'page:transition:finish' : 'page:finish'
return new Promise((resolve) => { return new Promise((resolve) => {
nuxtApp.hooks.hookOnce(hookToWait, async () => { nuxtApp.hooks.hookOnce(hookToWait, async () => {
await nextTick() await new Promise(resolve => setTimeout(resolve, 0))
if (to.hash) { if (to.hash) {
position = { el: to.hash, top: _getHashElementScrollMarginTop(to.hash), behavior } position = { el: to.hash, top: _getHashElementScrollMarginTop(to.hash), behavior }
} }