Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/nuxt/src/pages/runtime/router.options.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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 }
}

Expand Down Expand Up @@ -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
}