From 178a155eae4a4a3b6ab45d94e5c6728cbdc9e7e7 Mon Sep 17 00:00:00 2001 From: Johannes Schickling Date: Wed, 30 Mar 2022 14:23:01 +0000 Subject: [PATCH 1/2] fix: Prevent page to scroll to top during data HMR --- packages/next/client/next-dev.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/next/client/next-dev.js b/packages/next/client/next-dev.js index d3e4ad7c57547e7..a98a45213902acc 100644 --- a/packages/next/client/next-dev.js +++ b/packages/next/client/next-dev.js @@ -87,7 +87,8 @@ initialize({ webpackHMR }) new URLSearchParams(location.search) ) ), - router.asPath + router.asPath, + { scroll: false } ) .finally(clearIndicator) } From cc1f8a62377de5c3dc8c9b9606885b52caf672f1 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Wed, 30 Mar 2022 10:35:55 -0500 Subject: [PATCH 2/2] Add test for scroll position with gssp refreshing --- .../pages/another/index.js | 2 + .../test/index.test.ts | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/test/development/basic/gssp-ssr-change-reloading/pages/another/index.js b/test/development/basic/gssp-ssr-change-reloading/pages/another/index.js index 679405ddf80ada2..2e9e154507dccbd 100644 --- a/test/development/basic/gssp-ssr-change-reloading/pages/another/index.js +++ b/test/development/basic/gssp-ssr-change-reloading/pages/another/index.js @@ -9,6 +9,8 @@ export default function Gsp(props) { <>

change me

{JSON.stringify(props)}

+
+

bottom

) } diff --git a/test/development/basic/gssp-ssr-change-reloading/test/index.test.ts b/test/development/basic/gssp-ssr-change-reloading/test/index.test.ts index 828dc2adb2fe4e9..8d8cf6d6364319e 100644 --- a/test/development/basic/gssp-ssr-change-reloading/test/index.test.ts +++ b/test/development/basic/gssp-ssr-change-reloading/test/index.test.ts @@ -163,7 +163,44 @@ describe('GS(S)P Server-Side Change Reloading', () => { originalContent.replace('count = 1', 'count = 2') ) + await check( + async () => + JSON.parse(await browser.elementByCss('#props').text()).count + '', + '2' + ) + expect(await browser.eval('window.beforeChange')).toBe('hi') + await next.patchFile(page, originalContent) + }) + + it('should keep scroll position when updating from change in getStaticProps', async () => { + const browser = await webdriver(next.url, '/another') + await browser.eval( + 'document.getElementById("scroll-target").scrollIntoView()' + ) + const scrollPosition = await browser.eval( + 'document.documentElement.scrollTop' + ) + await browser.eval(`window.beforeChange = 'hi'`) + + const props = JSON.parse(await browser.elementByCss('#props').text()) + expect(props.count).toBe(1) + + const page = 'pages/another/index.js' + const originalContent = await next.readFile(page) + await next.patchFile( + page, + originalContent.replace('count = 1', 'count = 2') + ) + + await check( + async () => + JSON.parse(await browser.elementByCss('#props').text()).count + '', + '2' + ) expect(await browser.eval('window.beforeChange')).toBe('hi') + expect(await browser.eval('document.documentElement.scrollTop')).toBe( + scrollPosition + ) await next.patchFile(page, originalContent) })