Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chatty-pears-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: prevent infinite loop when calling `pushState`/`replaceState` in `$effect`
6 changes: 3 additions & 3 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BROWSER, DEV } from 'esm-env';
import { onMount, tick } from 'svelte';
import { onMount, tick, untrack } from 'svelte';
import {
decode_params,
decode_pathname,
Expand Down Expand Up @@ -2111,7 +2111,7 @@ export function pushState(url, state) {
page.state = state;
root.$set({
// we need to assign a new page object so that subscribers are correctly notified
page: clone_page(page)
page: untrack(() => clone_page(page))
});

clear_onward_history(current_history_index, current_navigation_index);
Expand Down Expand Up @@ -2154,7 +2154,7 @@ export function replaceState(url, state) {

page.state = state;
root.$set({
page: clone_page(page)
page: untrack(() => clone_page(page))
});
}

Expand Down
5 changes: 2 additions & 3 deletions packages/kit/test/apps/basics/src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ declare global {
}

interface PageState {
active: boolean;
active?: boolean;
count?: number;
}

interface Platform {}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script lang="ts">
import { pushState } from '$app/navigation';

let count = $state(0);

$effect(() => {
if (count) pushState('', { count });
});
</script>

<p>count: {count}</p>
<button onclick={() => count++}>Increment</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script lang="ts">
import { pushState as replaceState } from '$app/navigation';

let count = $state(0);

$effect(() => {
if (count) replaceState('', { count });
});
</script>

<p>count: {count}</p>
<button onclick={() => count++}>Increment</button>
14 changes: 14 additions & 0 deletions packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,20 @@ test.describe('Shallow routing', () => {
await expect(page.locator('h1')).toHaveText('parent');
await expect(page.locator('p')).toHaveText('active: true');
});

test('pushState does not loop infinitely in $effect', async ({ page }) => {
await page.goto('/shallow-routing/push-state/effect');
await expect(page.locator('p')).toHaveText('count: 0');
await page.locator('button').click();
await expect(page.locator('p')).toHaveText('count: 1');
});

test('replaceState does not loop infinitely in $effect', async ({ page }) => {
await page.goto('/shallow-routing/replace-state/effect');
await expect(page.locator('p')).toHaveText('count: 0');
await page.locator('button').click();
await expect(page.locator('p')).toHaveText('count: 1');
});
});

test.describe('reroute', () => {
Expand Down
Loading