Skip to content

Commit 477d860

Browse files
authored
prevent scroll when resetting page focus (#4065) (#4846)
* prevent scroll when resetting page focus * changeset
1 parent 2386534 commit 477d860

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

.changeset/olive-vans-talk.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Fix Safari scroll bug on ssr:false page reload

packages/kit/src/runtime/client/client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ export function create_client({ target, session, base, trailing_slash }) {
299299

300300
getSelection()?.removeAllRanges();
301301
root.tabIndex = -1;
302-
root.focus();
302+
root.focus({ preventScroll: true });
303303

304304
// restore `tabindex` as to prevent `root` from stealing input from elements
305305
if (tabindex !== null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<div class="container">
2+
<span> ^this is not the top of the screen</span>
3+
<div class="spacer" />
4+
</div>
5+
6+
<style>
7+
.container {
8+
margin-top: 300px;
9+
}
10+
11+
.spacer {
12+
height: 1000px;
13+
}
14+
</style>

packages/kit/test/apps/basics/test/test.js

+7
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ test.describe('Scrolling', () => {
304304
await back();
305305
expect(await page.evaluate(() => window.scrollY)).toBe(1000);
306306
});
307+
308+
test('scroll position is top of page on ssr:false reload', async ({ page }) => {
309+
await page.goto('/no-ssr/margin');
310+
expect(await page.evaluate(() => window.scrollY)).toBe(0);
311+
await page.reload();
312+
expect(await page.evaluate(() => window.scrollY)).toBe(0);
313+
});
307314
});
308315

309316
test.describe.parallel('Imports', () => {

0 commit comments

Comments
 (0)