Skip to content

Commit fbe4bcf

Browse files
authored
[fix] ignore popstate events from outside the router (#7721)
fixes #6914
1 parent 86626ba commit fbe4bcf

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

.changeset/ninety-colts-protect.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Ignore popstate events from outside the router

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,7 @@ export function create_client({ target, base, trailing_slash }) {
14581458
});
14591459

14601460
addEventListener('popstate', (event) => {
1461-
if (event.state) {
1461+
if (event.state?.[INDEX_KEY]) {
14621462
// if a popstate-driven navigation is cancelled, we need to counteract it
14631463
// with history.go, which means we end up back here, hence this check
14641464
if (event.state[INDEX_KEY] === current_history_index) return;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<h1>hello</h1>
2+
3+
<button
4+
on:click={() => {
5+
history.pushState({}, '', '/routing/external-popstate/does-not-exist');
6+
}}>go to /routing/external-popstate/does-not-exist</button
7+
>

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,20 @@ test.describe('Routing', () => {
791791
await close();
792792
}
793793
});
794+
795+
test('ignores popstate events from outside the router', async ({ page }) => {
796+
await page.goto('/routing/external-popstate');
797+
expect(await page.textContent('h1')).toBe('hello');
798+
799+
await page.click('button');
800+
expect(await page.textContent('h1')).toBe('hello');
801+
802+
await page.goBack();
803+
expect(await page.textContent('h1')).toBe('hello');
804+
805+
await page.goForward();
806+
expect(await page.textContent('h1')).toBe('hello');
807+
});
794808
});
795809

796810
test.describe('Shadow DOM', () => {

0 commit comments

Comments
 (0)