Skip to content

Commit

Permalink
fix: client state stale after invalidate (#9798)
Browse files Browse the repository at this point in the history
fixes #9796
  • Loading branch information
gtm-nayan committed May 5, 2023
1 parent 45a8f75 commit fbd628e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-beds-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: update `$page.data` correctly after invalidate
3 changes: 3 additions & 0 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ export function create_client(app, target) {
if (navigation_result.type === 'redirect') {
return goto(new URL(navigation_result.location, url).href, {}, [url.pathname], nav_token);
} else {
if (navigation_result.props.page !== undefined) {
page = navigation_result.props.page;
}
root.$set(navigation_result.props);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { error } from '@sveltejs/kit';

/** @type {import('./$types').PageServerLoad} */
export function load() {
export function load({ cookies }) {
const enhance_counter = +(cookies.get('enhance-counter') ?? 0);

return {
initial: 'initial'
initial: 'initial',
enhance_counter
};
}

Expand Down Expand Up @@ -39,5 +42,16 @@ export const actions = {
return {
message: data.get('message')
};
},
counter: async ({ cookies }) => {
let count = +(cookies.get('enhance-counter') ?? 0);

count += 1;

cookies.set('enhance-counter', count + '', {
path: '/actions/enhance'
});

return {};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
/** @type {import('./$types').ActionData} */
export let form;
/** @type {import('./$types').PageData} */
export let data;
/** @type {AbortController | undefined} */
let previous;
let count = 0;
</script>

<pre class="data1">prop: {data.enhance_counter}, store: {$page.data.enhance_counter}</pre>

<pre class="formdata1">{JSON.stringify(form)}</pre>
<pre class="formdata2">{JSON.stringify($page.form)}</pre>

Expand Down Expand Up @@ -42,3 +47,7 @@
<input name="message" type="text" value={form?.message ?? ''} />
<button class="form3">Submit</button>
</form>

<form action="?/counter" method="post" use:enhance>
<button class="form4">Submit</button>
</form>
13 changes: 13 additions & 0 deletions packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -775,3 +775,16 @@ test.describe('Streaming', () => {
});
}
});

test.describe('Actions', () => {
test('page store has correct data', async ({ page }) => {
await page.goto('/actions/enhance');
const pre = page.locator('pre.data1');

await expect(pre).toHaveText(`prop: 0, store: 0`);
await page.locator('.form4').click();
await expect(pre).toHaveText(`prop: 1, store: 1`);
await page.evaluate('window.svelte_tick()');
await expect(pre).toHaveText(`prop: 1, store: 1`);
});
});
2 changes: 1 addition & 1 deletion packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ test.describe('Actions', () => {
await page.goto('/actions/enhance');
}

expect(await page.textContent('pre')).toBe(JSON.stringify(null));
expect(await page.textContent('pre.formdata1')).toBe(JSON.stringify(null));
});

test('applyAction redirects', async ({ page, javaScriptEnabled }) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/kit/test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
beforeNavigate,
afterNavigate
} from '$app/navigation';
import { onMount } from 'svelte';
import { onMount, tick } from 'svelte';

export function setup() {
onMount(() => {
Expand All @@ -17,7 +17,8 @@ export function setup() {
preloadCode,
preloadData,
beforeNavigate,
afterNavigate
afterNavigate,
svelte_tick: tick
});

// communicate that the app is ready
Expand Down

0 comments on commit fbd628e

Please sign in to comment.