Skip to content

Commit

Permalink
fix(nuxt): use setTimeout before scrolling when navigating (#25817)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chalks committed Feb 22, 2024
1 parent 6b50425 commit 0152cf4
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/nuxt/src/pages/runtime/router.options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { RouteLocationNormalized, RouterScrollBehavior } from '#vue-router'
import { nextTick } from 'vue'
import type { RouterConfig } from 'nuxt/schema'
import { useNuxtApp } from '#app/nuxt'
import { isChangingPage } from '#app/components/utils'
Expand Down Expand Up @@ -36,14 +35,16 @@ export default <RouterConfig> {
if (to.hash) {
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
const hasTransition = (route: RouteLocationNormalized) => !!(route.meta.pageTransition ?? defaultPageTransition)
const hookToWait = (hasTransition(from) && hasTransition(to)) ? 'page:transition:finish' : 'page:finish'
return new Promise((resolve) => {
nuxtApp.hooks.hookOnce(hookToWait, async () => {
await nextTick()
await new Promise(resolve => setTimeout(resolve, 0))
if (to.hash) {
position = { el: to.hash, top: _getHashElementScrollMarginTop(to.hash), behavior }
}
Expand Down

0 comments on commit 0152cf4

Please sign in to comment.