From 633ec12956fa0b85829c93e18d46f466ce064c7d Mon Sep 17 00:00:00 2001 From: Kanta Date: Fri, 22 Jan 2021 18:46:14 +0900 Subject: [PATCH 1/2] Fix shallow routing scroll --- packages/next/next-server/lib/router/router.ts | 5 ++++- .../pages/nav/shallow-routing.js | 4 +++- .../client-navigation/test/index.test.js | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index c5f464b15f08ff2..6238e1c139aaea0 100644 --- a/packages/next/next-server/lib/router/router.ts +++ b/packages/next/next-server/lib/router/router.ts @@ -1093,13 +1093,16 @@ export default class Router implements BaseRouter { !(routeInfo.Component as any).getInitialProps } + // shallow routing is only allowed for same page URL changes. + const isValidShallowRoute = options.shallow && this.route === route await this.set( route, pathname!, query, cleanedAs, routeInfo, - forcedScroll || (options.scroll ? { x: 0, y: 0 } : null) + forcedScroll || + (isValidShallowRoute || !options.scroll ? null : { x: 0, y: 0 }) ).catch((e) => { if (e.cancelled) error = error || e else throw e diff --git a/test/integration/client-navigation/pages/nav/shallow-routing.js b/test/integration/client-navigation/pages/nav/shallow-routing.js index 61dc8032249c1fb..7d41faa1df8bf54 100644 --- a/test/integration/client-navigation/pages/nav/shallow-routing.js +++ b/test/integration/client-navigation/pages/nav/shallow-routing.js @@ -35,7 +35,9 @@ export default class extends Component { Home -
Counter: {this.getCurrentCounter()}
+
+ Counter: {this.getCurrentCounter()} +
getInitialProps run count: {this.props.getInitialPropsRunCount}
diff --git a/test/integration/client-navigation/test/index.test.js b/test/integration/client-navigation/test/index.test.js index d6c00e0bbfd57f7..e3b951a3927e7b9 100644 --- a/test/integration/client-navigation/test/index.test.js +++ b/test/integration/client-navigation/test/index.test.js @@ -854,6 +854,21 @@ describe('Client Navigation', () => { await browser.close() }) + + it('should keep the scroll position on shallow routing', async () => { + const browser = await webdriver(context.appPort, '/nav/shallow-routing') + await browser.eval(() => + document.querySelector('#increase').scrollIntoView() + ) + const scrollPosition = await browser.eval('window.pageYOffset') + + const newScrollPosition = await browser + .elementByCss('#increase') + .click() + .eval('window.pageYOffset') + + expect(newScrollPosition).toBe(scrollPosition) + }) }) describe('with URL objects', () => { From 50d28b2a22fe8d1d320595317f06dc1ceb7f33df Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Fri, 22 Jan 2021 12:34:55 -0500 Subject: [PATCH 2/2] new tests --- .../pages/nav/shallow-routing.js | 17 +++++++++++ .../client-navigation/test/index.test.js | 28 ++++++++++++++++--- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/test/integration/client-navigation/pages/nav/shallow-routing.js b/test/integration/client-navigation/pages/nav/shallow-routing.js index 7d41faa1df8bf54..728467f9afde42b 100644 --- a/test/integration/client-navigation/pages/nav/shallow-routing.js +++ b/test/integration/client-navigation/pages/nav/shallow-routing.js @@ -27,6 +27,17 @@ export default class extends Component { Router.push(href, href, { shallow: true }) } + increaseNonShallow() { + const counter = this.getCurrentCounter() + const href = `/nav/shallow-routing?counter=${counter + 1}` + Router.push(href, href, {}) + } + + gotoNavShallow() { + const href = `/nav` + Router.push(href, href, { shallow: true }) + } + render() { return (
@@ -44,6 +55,12 @@ export default class extends Component { + +
) } diff --git a/test/integration/client-navigation/test/index.test.js b/test/integration/client-navigation/test/index.test.js index e3b951a3927e7b9..0d87cdeda148056 100644 --- a/test/integration/client-navigation/test/index.test.js +++ b/test/integration/client-navigation/test/index.test.js @@ -862,12 +862,32 @@ describe('Client Navigation', () => { ) const scrollPosition = await browser.eval('window.pageYOffset') - const newScrollPosition = await browser - .elementByCss('#increase') - .click() - .eval('window.pageYOffset') + expect(scrollPosition).toBeGreaterThan(3000) + + await browser.elementByCss('#increase').click() + await waitFor(500) + const newScrollPosition = await browser.eval('window.pageYOffset') expect(newScrollPosition).toBe(scrollPosition) + + await browser.elementByCss('#increase2').click() + await waitFor(500) + const newScrollPosition2 = await browser.eval('window.pageYOffset') + + expect(newScrollPosition2).toBe(0) + + await browser.eval(() => + document.querySelector('#invalidShallow').scrollIntoView() + ) + const scrollPositionDown = await browser.eval('window.pageYOffset') + + expect(scrollPositionDown).toBeGreaterThan(3000) + + await browser.elementByCss('#invalidShallow').click() + await waitFor(500) + const newScrollPosition3 = await browser.eval('window.pageYOffset') + + expect(newScrollPosition3).toBe(0) }) })