Skip to content

Commit c64c0da

Browse files
committed
block reactivity
1 parent f9ec267 commit c64c0da

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
@@ -2216,7 +2216,7 @@ export function pushState(url, state) {
22162216
history.pushState(opts, '', resolve_url(url));
22172217
has_navigated = true;
22182218

2219-
page.state = state;
2219+
page.state = unstringify(opts[STATES_KEY], app.hooks.transport);
22202220
root.$set({
22212221
// we need to assign a new page object so that subscribers are correctly notified
22222222
page: untrack(() => clone_page(page))
@@ -2250,7 +2250,7 @@ export function replaceState(url, state) {
22502250

22512251
history.replaceState(opts, '', resolve_url(url));
22522252

2253-
page.state = state;
2253+
page.state = unstringify(opts[STATES_KEY], app.hooks.transport);
22542254
root.$set({
22552255
page: untrack(() => clone_page(page))
22562256
});

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
@@ -1624,13 +1624,20 @@ test.describe('Shallow routing', () => {
16241624

16251625
test('pushState properly serializes objects', async ({ page }) => {
16261626
await page.goto('/shallow-routing/push-state/foo');
1627-
await expect(page.locator('p')).toHaveText('foo: nope');
1627+
await expect(page.locator('[data-testid=foo]')).toHaveText('foo: nope');
1628+
await expect(page.locator('[data-testid=count]')).toHaveText('count: nope');
16281629

1629-
await page.locator('button').click();
1630-
await expect(page.locator('p')).toHaveText('foo: it works?!');
1630+
await page.getByText('push state').click();
1631+
await expect(page.locator('[data-testid=foo]')).toHaveText('foo: it works?!');
1632+
await expect(page.locator('[data-testid=count]')).toHaveText('count: 0');
1633+
1634+
await page.getByText('bump count').click();
1635+
await expect(page.locator('[data-testid=foo]')).toHaveText('foo: it works?!');
1636+
await expect(page.locator('[data-testid=count]')).toHaveText('count: 0'); // Ensure count is not bumped
16311637

16321638
await page.goBack();
16331639
await expect(page.locator('p')).toHaveText('foo: nope');
1640+
await expect(page.locator('[data-testid=count]')).toHaveText('count: nope');
16341641
});
16351642
});
16361643

0 commit comments

Comments
 (0)