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 @@ + + + + + diff --git a/test/fixtures/basic/pages/index.vue b/test/fixtures/basic/pages/index.vue index 3d7d5fe2f0..18f5dcf355 100644 --- a/test/fixtures/basic/pages/index.vue +++ b/test/fixtures/basic/pages/index.vue @@ -53,6 +53,9 @@ + + to big 1 + diff --git a/test/fixtures/basic/pages/nested/[foo]/index.vue b/test/fixtures/basic/pages/nested/[foo].vue similarity index 56% rename from test/fixtures/basic/pages/nested/[foo]/index.vue rename to test/fixtures/basic/pages/nested/[foo].vue index beab2f220e..1afdd3b4ab 100644 --- a/test/fixtures/basic/pages/nested/[foo]/index.vue +++ b/test/fixtures/basic/pages/nested/[foo].vue @@ -6,5 +6,16 @@ const route = useRoute('nested-foo')
nested/[foo]/index.vue
foo: {{ route.params.foo }}
+ +
+
+
+ + diff --git a/test/fixtures/basic/pages/nested/[foo]/[bar].vue b/test/fixtures/basic/pages/nested/[foo]/[bar].vue index 168f7737dd..a0dce6983d 100644 --- a/test/fixtures/basic/pages/nested/[foo]/[bar].vue +++ b/test/fixtures/basic/pages/nested/[foo]/[bar].vue @@ -7,5 +7,11 @@ const route = useRoute('nested-foo-bar')
nested/[foo]/[bar].vue
foo: {{ route.params.foo }}
bar: {{ route.params.bar }}
+ + to /nested/foo/user-test + + + to /nested/foo/test +
diff --git a/test/fixtures/basic/pages/nested/[foo]/user-[group].vue b/test/fixtures/basic/pages/nested/[foo]/user-[group].vue index 4a4da9fe55..9847e51563 100644 --- a/test/fixtures/basic/pages/nested/[foo]/user-[group].vue +++ b/test/fixtures/basic/pages/nested/[foo]/user-[group].vue @@ -7,5 +7,12 @@ const route = useRoute('nested-foo-user-group')
nested/[foo]/user-[group].vue
foo: {{ route.params.foo }}
group: {{ route.params.group }}
+ + + to /nested/foo/user-test + + + to /nested/foo/test +