fix(nuxt): scroll to top on dynamic routes with different params (#8327)

Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
Sébastien Chopin 2022-10-19 16:33:01 +02:00 committed by GitHub
parent 418b5671ff
commit 0106e09e32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 6 deletions

View File

@ -1,5 +1,6 @@
import type { RouterConfig } from '@nuxt/schema'
import type { RouterScrollBehavior } from 'vue-router'
import type { RouterScrollBehavior, RouteLocationNormalized } from 'vue-router'
import { isEqual } from 'ohash'
import { nextTick } from 'vue'
import { useNuxtApp } from '#app'
@ -16,11 +17,7 @@ export default <RouterConfig> {
let position: ScrollPosition = savedPosition || undefined
// Scroll to top if route is changed by default
if (
!position &&
(from && to && from.matched[0] !== to.matched[0]) &&
to.meta.scrollToTop !== false
) {
if (!position && from && to && to.meta.scrollToTop !== false && _isDifferentRoute(from, to)) {
position = { left: 0, top: 0 }
}
@ -56,3 +53,14 @@ function _getHashElementScrollMarginTop (selector: string): number {
}
return 0
}
function _isDifferentRoute (a: RouteLocationNormalized, b: RouteLocationNormalized): boolean {
const samePageComponent = a.matched[0] === b.matched[0]
if (!samePageComponent) {
return true
}
if (samePageComponent && !isEqual(a.params, b.params)) {
return true
}
return false
}