From 7fd0807f49c3deafe8619a6a057ddeb51a38e9f6 Mon Sep 17 00:00:00 2001 From: Rafael Lebre Date: Thu, 27 Aug 2020 19:12:53 -0300 Subject: [PATCH 1/5] Fix #top anchor link scroll --- packages/next/next-server/lib/router/router.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index e0cf3552e9172..a7070b7149c5b 100644 --- a/packages/next/next-server/lib/router/router.ts +++ b/packages/next/next-server/lib/router/router.ts @@ -840,8 +840,9 @@ export default class Router implements BaseRouter { scrollToHash(as: string): void { const [, hash] = as.split('#') - // Scroll to top if the hash is just `#` with no value - if (hash === '') { + // Scroll to top if the hash is just `#` with no value or `#top` + // To mirror browsers + if (hash === '' || hash === 'top') { window.scrollTo(0, 0) return } From f2dbd6ae7d7530765e0c1b17464fc594b0a25b59 Mon Sep 17 00:00:00 2001 From: Rafael Lebre Date: Tue, 1 Sep 2020 08:13:50 -0300 Subject: [PATCH 2/5] add test for #top anchor scroll --- .../client-navigation/pages/nav/hash-changes.js | 3 +++ test/integration/client-navigation/test/index.test.js | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/test/integration/client-navigation/pages/nav/hash-changes.js b/test/integration/client-navigation/pages/nav/hash-changes.js index fb87ee235fb89..e76f3a22d89fc 100644 --- a/test/integration/client-navigation/pages/nav/hash-changes.js +++ b/test/integration/client-navigation/pages/nav/hash-changes.js @@ -42,6 +42,9 @@ const HashChanges = ({ count }) => { ) })} + + Via Top Hash +
ASPATH: {router.asPath}
PATHNAME: {router.pathname}
diff --git a/test/integration/client-navigation/test/index.test.js b/test/integration/client-navigation/test/index.test.js index 1e2c7c8bac347..529fcd50e4595 100644 --- a/test/integration/client-navigation/test/index.test.js +++ b/test/integration/client-navigation/test/index.test.js @@ -479,6 +479,14 @@ describe('Client Navigation', () => { .eval('window.pageYOffset') expect(scrollPositionAfterEmptyHash).toBe(0) + + // Scrolls back to top when clicking link with href `#top`. + const scrollPositionAfterTopHash = await browser + .elementByCss('#via-top-hash') + .click() + .eval('window.pageYOffset') + + expect(scrollPositionAfterTopHash).toBe(0) } finally { if (browser) { await browser.close() From cea3c8ebdb0d512a42be67f6da96183879cc3568 Mon Sep 17 00:00:00 2001 From: Rafael Lebre Date: Mon, 21 Sep 2020 16:10:14 -0300 Subject: [PATCH 3/5] fix test for #top anchor scroll --- .../integration/client-navigation/test/index.test.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test/integration/client-navigation/test/index.test.js b/test/integration/client-navigation/test/index.test.js index 529fcd50e4595..482cd22aa34a8 100644 --- a/test/integration/client-navigation/test/index.test.js +++ b/test/integration/client-navigation/test/index.test.js @@ -465,12 +465,12 @@ describe('Client Navigation', () => { browser = await webdriver(context.appPort, '/nav/hash-changes') // Scrolls to item 400 on the page - const scrollPosition = await browser + const scrollPositionBeforeEmptyHash = await browser .elementByCss('#scroll-to-item-400') .click() .eval('window.pageYOffset') - expect(scrollPosition).toBe(7258) + expect(scrollPositionBeforeEmptyHash).toBe(7258) // Scrolls back to top when scrolling to `#` with no value. const scrollPositionAfterEmptyHash = await browser @@ -480,6 +480,14 @@ describe('Client Navigation', () => { expect(scrollPositionAfterEmptyHash).toBe(0) + // Scrolls to item 400 on the page + const scrollPositionBeforeTopHash = await browser + .elementByCss('#scroll-to-item-400') + .click() + .eval('window.pageYOffset') + + expect(scrollPositionBeforeTopHash).toBe(7258) + // Scrolls back to top when clicking link with href `#top`. const scrollPositionAfterTopHash = await browser .elementByCss('#via-top-hash') From ea596a61585a86b1a887aa5c6a42594e8b698021 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Fri, 29 Jan 2021 14:49:42 -0500 Subject: [PATCH 4/5] fix test --- test/integration/build-output/test/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/build-output/test/index.test.js b/test/integration/build-output/test/index.test.js index cf8eca1c83ff2..468cc1fc6bb0b 100644 --- a/test/integration/build-output/test/index.test.js +++ b/test/integration/build-output/test/index.test.js @@ -104,7 +104,7 @@ describe('Build Output', () => { expect(parseFloat(err404FirstLoad)).toBeCloseTo(67, 1) expect(err404FirstLoad.endsWith('kB')).toBe(true) - expect(parseFloat(sharedByAll)).toBeCloseTo(63.5, 1) + expect(parseFloat(sharedByAll)).toBeCloseTo(63.6, 1) expect(sharedByAll.endsWith('kB')).toBe(true) if (_appSize.endsWith('kB')) { From aea8a3feb3a67334bfd1db56ebe9a0054860dbb5 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Fri, 29 Jan 2021 23:09:13 -0500 Subject: [PATCH 5/5] Restore index.test.js --- test/integration/build-output/test/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/build-output/test/index.test.js b/test/integration/build-output/test/index.test.js index 468cc1fc6bb0b..cf8eca1c83ff2 100644 --- a/test/integration/build-output/test/index.test.js +++ b/test/integration/build-output/test/index.test.js @@ -104,7 +104,7 @@ describe('Build Output', () => { expect(parseFloat(err404FirstLoad)).toBeCloseTo(67, 1) expect(err404FirstLoad.endsWith('kB')).toBe(true) - expect(parseFloat(sharedByAll)).toBeCloseTo(63.6, 1) + expect(parseFloat(sharedByAll)).toBeCloseTo(63.5, 1) expect(sharedByAll.endsWith('kB')).toBe(true) if (_appSize.endsWith('kB')) {