diff --git a/packages/nuxt/src/pages/runtime/router.options.ts b/packages/nuxt/src/pages/runtime/router.options.ts
index 42de00df3d..8703a3b518 100644
--- a/packages/nuxt/src/pages/runtime/router.options.ts
+++ b/packages/nuxt/src/pages/runtime/router.options.ts
@@ -63,13 +63,5 @@ function _getHashElementScrollMarginTop (selector: string): number {
}
function _isDifferentRoute (from: RouteLocationNormalized, to: RouteLocationNormalized): boolean {
- const samePageComponent = to.matched.every((comp, index) => comp.components?.default === from.matched[index]?.components?.default)
-
- if (!samePageComponent) {
- return true
- }
- if (samePageComponent && JSON.stringify(from.params) !== JSON.stringify(to.params)) {
- return true
- }
- return false
+ return to.path !== from.path || JSON.stringify(from.params) !== JSON.stringify(to.params)
}
diff --git a/test/basic.test.ts b/test/basic.test.ts
index 1f1df4b479..152b812d84 100644
--- a/test/basic.test.ts
+++ b/test/basic.test.ts
@@ -533,6 +533,56 @@ describe('nuxt links', () => {
await page.close()
})
+
+ it('expect scroll to top on routes with same component', async () => {
+ // #22402
+ const page = await createPage('/big-page-1')
+ await page.setViewportSize({
+ width: 1000,
+ height: 1000
+ })
+ await page.waitForLoadState('networkidle')
+
+ await page.locator('#big-page-2').scrollIntoViewIfNeeded()
+ expect(await page.evaluate(() => window.scrollY)).toBeGreaterThan(0)
+ await page.locator('#big-page-2').click()
+ await page.waitForURL(url => url.href.includes('/big-page-2'))
+ await page.waitForTimeout(25)
+ expect(await page.evaluate(() => window.scrollY)).toBe(0)
+
+ await page.locator('#big-page-1').scrollIntoViewIfNeeded()
+ expect(await page.evaluate(() => window.scrollY)).toBeGreaterThan(0)
+ await page.locator('#big-page-1').click()
+ await page.waitForURL(url => url.href.includes('/big-page-1'))
+ await page.waitForTimeout(25)
+ expect(await page.evaluate(() => window.scrollY)).toBe(0)
+ await page.close()
+ })
+
+ it('expect scroll to top on nested pages', async () => {
+ // #20523
+ const page = await createPage('/nested/foo/test')
+ await page.setViewportSize({
+ width: 1000,
+ height: 1000
+ })
+ await page.waitForLoadState('networkidle')
+
+ await page.locator('#user-test').scrollIntoViewIfNeeded()
+ expect(await page.evaluate(() => window.scrollY)).toBeGreaterThan(0)
+ await page.locator('#user-test').click()
+ await page.waitForURL(url => url.href.includes('/nested/foo/user-test'))
+ await page.waitForTimeout(25)
+ expect(await page.evaluate(() => window.scrollY)).toBe(0)
+
+ await page.locator('#test').scrollIntoViewIfNeeded()
+ expect(await page.evaluate(() => window.scrollY)).toBeGreaterThan(0)
+ await page.locator('#test').click()
+ await page.waitForURL(url => url.href.includes('/nested/foo/test'))
+ await page.waitForTimeout(25)
+ expect(await page.evaluate(() => window.scrollY)).toBe(0)
+ await page.close()
+ })
})
describe('head tags', () => {
diff --git a/test/fixtures/basic/modules/page-extend.ts b/test/fixtures/basic/modules/page-extend/index.ts
similarity index 60%
rename from test/fixtures/basic/modules/page-extend.ts
rename to test/fixtures/basic/modules/page-extend/index.ts
index 1f794eeee6..62723a909d 100644
--- a/test/fixtures/basic/modules/page-extend.ts
+++ b/test/fixtures/basic/modules/page-extend/index.ts
@@ -12,7 +12,13 @@ export default defineNuxtModule({
pages.push({
name: 'page-extend',
path: '/page-extend',
- file: resolver.resolve('./runtime/page.vue')
+ file: resolver.resolve('../runtime/page.vue')
+ }, {
+ path: '/big-page-1',
+ file: resolver.resolve('./pages/big-page.vue')
+ }, {
+ path: '/big-page-2',
+ file: resolver.resolve('./pages/big-page.vue')
})
})
}
diff --git a/test/fixtures/basic/modules/page-extend/pages/big-page.vue b/test/fixtures/basic/modules/page-extend/pages/big-page.vue
new file mode 100644
index 0000000000..9e448d9a55
--- /dev/null
+++ b/test/fixtures/basic/modules/page-extend/pages/big-page.vue
@@ -0,0 +1,36 @@
+
+