Skip to content

Commit 050936c

Browse files
authored
test(fix: Prevent page to scroll to top during data HMR) (#35744)
* fix: Prevent page to scroll to top during data HMR * Add test for scroll position with gssp refreshing
1 parent 600e0b3 commit 050936c

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

test/development/basic/gssp-ssr-change-reloading/pages/another/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export default function Gsp(props) {
99
<>
1010
<p id="change">change me</p>
1111
<p id="props">{JSON.stringify(props)}</p>
12+
<div style={{ height: 1000, width: 50, background: 'cyan' }}></div>
13+
<p id="scroll-target">bottom</p>
1214
</>
1315
)
1416
}

test/development/basic/gssp-ssr-change-reloading/test/index.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,44 @@ describe('GS(S)P Server-Side Change Reloading', () => {
163163
originalContent.replace('count = 1', 'count = 2')
164164
)
165165

166+
await check(
167+
async () =>
168+
JSON.parse(await browser.elementByCss('#props').text()).count + '',
169+
'2'
170+
)
171+
expect(await browser.eval('window.beforeChange')).toBe('hi')
172+
await next.patchFile(page, originalContent)
173+
})
174+
175+
it('should keep scroll position when updating from change in getStaticProps', async () => {
176+
const browser = await webdriver(next.url, '/another')
177+
await browser.eval(
178+
'document.getElementById("scroll-target").scrollIntoView()'
179+
)
180+
const scrollPosition = await browser.eval(
181+
'document.documentElement.scrollTop'
182+
)
183+
await browser.eval(`window.beforeChange = 'hi'`)
184+
185+
const props = JSON.parse(await browser.elementByCss('#props').text())
186+
expect(props.count).toBe(1)
187+
188+
const page = 'pages/another/index.js'
189+
const originalContent = await next.readFile(page)
190+
await next.patchFile(
191+
page,
192+
originalContent.replace('count = 1', 'count = 2')
193+
)
194+
195+
await check(
196+
async () =>
197+
JSON.parse(await browser.elementByCss('#props').text()).count + '',
198+
'2'
199+
)
166200
expect(await browser.eval('window.beforeChange')).toBe('hi')
201+
expect(await browser.eval('document.documentElement.scrollTop')).toBe(
202+
scrollPosition
203+
)
167204
await next.patchFile(page, originalContent)
168205
})
169206

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy