Skip to content

Commit d75268c

Browse files
committed
block reactivity
1 parent 7e313d5 commit d75268c

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,7 +2166,7 @@ export function pushState(url, state) {
21662166
history.pushState(opts, '', resolve_url(url));
21672167
has_navigated = true;
21682168

2169-
page.state = state;
2169+
page.state = unstringify(opts[STATES_KEY], app.hooks.transport);
21702170
root.$set({
21712171
// we need to assign a new page object so that subscribers are correctly notified
21722172
page: untrack(() => clone_page(page))
@@ -2200,7 +2200,7 @@ export function replaceState(url, state) {
22002200

22012201
history.replaceState(opts, '', resolve_url(url));
22022202

2203-
page.state = state;
2203+
page.state = unstringify(opts[STATES_KEY], app.hooks.transport);
22042204
root.$set({
22052205
page: untrack(() => clone_page(page))
22062206
});

packages/kit/test/apps/basics/src/routes/shallow-routing/push-state/foo/+page.svelte

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@
44
import { Foo } from '$lib';
55
</script>
66

7-
<button onclick={() => pushState('', { foo: new Foo('it works?') })}>do the thing</button>
7+
<button
8+
onclick={() => {
9+
const state = $state({ foo: new Foo('it works?'), count: 0 });
10+
pushState('', state);
11+
}}>push state</button
12+
>
813

9-
<p>foo: {page.state.foo?.bar() ?? 'nope'}</p>
14+
<!-- Make sure that page.state.count loses its reactivity -->
15+
<button
16+
onclick={() => {
17+
page.state.count++;
18+
}}
19+
>
20+
bump count
21+
</button>
22+
23+
<p data-testid="foo">foo: {page.state.foo?.bar() ?? 'nope'}</p>
24+
<p data-testid="count">count: {page.state.count ?? 'nope'}</p>

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,13 +1500,20 @@ test.describe('Shallow routing', () => {
15001500

15011501
test('pushState properly serializes objects', async ({ page }) => {
15021502
await page.goto('/shallow-routing/push-state/foo');
1503-
await expect(page.locator('p')).toHaveText('foo: nope');
1503+
await expect(page.locator('[data-testid=foo]')).toHaveText('foo: nope');
1504+
await expect(page.locator('[data-testid=count]')).toHaveText('count: nope');
15041505

1505-
await page.locator('button').click();
1506-
await expect(page.locator('p')).toHaveText('foo: it works?!');
1506+
await page.getByText('push state').click();
1507+
await expect(page.locator('[data-testid=foo]')).toHaveText('foo: it works?!');
1508+
await expect(page.locator('[data-testid=count]')).toHaveText('count: 0');
1509+
1510+
await page.getByText('bump count').click();
1511+
await expect(page.locator('[data-testid=foo]')).toHaveText('foo: it works?!');
1512+
await expect(page.locator('[data-testid=count]')).toHaveText('count: 0'); // Ensure count is not bumped
15071513

15081514
await page.goBack();
15091515
await expect(page.locator('p')).toHaveText('foo: nope');
1516+
await expect(page.locator('[data-testid=count]')).toHaveText('count: nope');
15101517
});
15111518
});
15121519

0 commit comments

Comments
 (0)