fix(nuxt): use shared isChangingPage util in scrollBehavior (#24091)

This commit is contained in:
Daniel Roe 2023-11-02 20:51:32 +01:00 committed by GitHub
parent dc04f28cff
commit d1306d6ebd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ import type { RouteLocationNormalized, RouterScrollBehavior } from '#vue-router'
import { nextTick } from 'vue' 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 { useRouter } from '#app/composables/router' import { useRouter } from '#app/composables/router'
// @ts-expect-error virtual file // @ts-expect-error virtual file
import { appPageTransition as defaultPageTransition } from '#build/nuxt.config.mjs' import { appPageTransition as defaultPageTransition } from '#build/nuxt.config.mjs'
@ -23,7 +24,7 @@ export default <RouterConfig> {
const routeAllowsScrollToTop = typeof to.meta.scrollToTop === 'function' ? to.meta.scrollToTop(to, from) : to.meta.scrollToTop const routeAllowsScrollToTop = typeof to.meta.scrollToTop === 'function' ? to.meta.scrollToTop(to, from) : to.meta.scrollToTop
// Scroll to top if route is changed by default // Scroll to top if route is changed by default
if (!position && from && to && routeAllowsScrollToTop !== false && _isDifferentRoute(from, to)) { if (!position && from && to && routeAllowsScrollToTop !== false && isChangingPage(to, from)) {
position = { left: 0, top: 0 } position = { left: 0, top: 0 }
} }
@ -61,7 +62,3 @@ function _getHashElementScrollMarginTop (selector: string): number {
} catch {} } catch {}
return 0 return 0
} }
function _isDifferentRoute (from: RouteLocationNormalized, to: RouteLocationNormalized): boolean {
return to.path !== from.path || JSON.stringify(from.params) !== JSON.stringify(to.params)
}