Skip to content

Commit d14758d

Browse files
authored
[feat] add form prop to page store (#6986)
Closes #6701
1 parent 560394f commit d14758d

File tree

6 files changed

+29
-14
lines changed

6 files changed

+29
-14
lines changed

.changeset/healthy-lions-attend.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+
Added form property to \$page store

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,11 @@ export function create_client({ target, base, trailing_slash }) {
438438
}
439439

440440
const page_changed =
441-
!current.url || url.href !== current.url.href || current.error !== error || data_changed;
441+
!current.url ||
442+
url.href !== current.url.href ||
443+
current.error !== error ||
444+
form !== undefined ||
445+
data_changed;
442446

443447
if (page_changed) {
444448
result.props.page = {
@@ -447,6 +451,7 @@ export function create_client({ target, base, trailing_slash }) {
447451
routeId: route && route.id,
448452
status,
449453
url,
454+
form,
450455
// The whole page store is updated, but this way the object reference stays the same
451456
data: data_changed ? data : page.data
452457
};
@@ -1203,13 +1208,10 @@ export function create_client({ target, base, trailing_slash }) {
12031208
goto(result.location, {}, []);
12041209
} else {
12051210
/** @type {Record<string, any>} */
1206-
const props = { form: result.data };
1207-
1208-
if (result.status !== page.status) {
1209-
page = { ...page, status: result.status };
1210-
props.page = page;
1211-
}
1212-
1211+
const props = {
1212+
form: result.data,
1213+
page: { ...page, form: result.data, status: result.status }
1214+
};
12131215
const post_update = pre_update();
12141216
root.$set(props);
12151217
post_update();

packages/kit/src/runtime/server/page/render.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ export async function render_response({
9898
routeId: event.routeId,
9999
status,
100100
url: event.url,
101-
data
101+
data,
102+
form: form_value
102103
};
103104

104105
// TODO remove this for 1.0

packages/kit/test/apps/basics/src/routes/actions/enhance/+page.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script>
22
import { enhance } from '$app/forms';
3+
import { page } from '$app/stores';
34
45
/** @type {import('./$types').ActionData} */
56
export let form;
@@ -9,7 +10,8 @@
910
let count = 0;
1011
</script>
1112

12-
<pre>{JSON.stringify(form)}</pre>
13+
<pre class="formdata1">{JSON.stringify(form)}</pre>
14+
<pre class="formdata2">{JSON.stringify($page.form)}</pre>
1315

1416
<form method="post" action="?/login" use:enhance>
1517
<input name="username" type="text" />

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,15 +1819,17 @@ test.describe('Actions', () => {
18191819
test('use:enhance', async ({ page }) => {
18201820
await page.goto('/actions/enhance');
18211821

1822-
expect(await page.textContent('pre')).toBe(JSON.stringify(null));
1822+
expect(await page.textContent('pre.formdata1')).toBe(JSON.stringify(null));
1823+
expect(await page.textContent('pre.formdata2')).toBe(JSON.stringify(null));
18231824

18241825
await page.type('input[name="username"]', 'foo');
18251826
await Promise.all([
18261827
page.waitForRequest((request) => request.url().includes('/actions/enhance')),
18271828
page.click('button.form1')
18281829
]);
18291830

1830-
await expect(page.locator('pre')).toHaveText(JSON.stringify({ result: 'foo' }));
1831+
await expect(page.locator('pre.formdata1')).toHaveText(JSON.stringify({ result: 'foo' }));
1832+
await expect(page.locator('pre.formdata2')).toHaveText(JSON.stringify({ result: 'foo' }));
18311833
});
18321834

18331835
test('use:enhance abort controller', async ({ page, javaScriptEnabled }) => {
@@ -1850,15 +1852,17 @@ test.describe('Actions', () => {
18501852
test('use:enhance button with formAction', async ({ page, app }) => {
18511853
await page.goto('/actions/enhance');
18521854

1853-
expect(await page.textContent('pre')).toBe(JSON.stringify(null));
1855+
expect(await page.textContent('pre.formdata1')).toBe(JSON.stringify(null));
18541856

18551857
await page.type('input[name="username"]', 'foo');
18561858
await Promise.all([
18571859
page.waitForRequest((request) => request.url().includes('/actions/enhance')),
18581860
page.click('button.form1-register')
18591861
]);
18601862

1861-
await expect(page.locator('pre')).toHaveText(JSON.stringify({ result: 'register: foo' }));
1863+
await expect(page.locator('pre.formdata1')).toHaveText(
1864+
JSON.stringify({ result: 'register: foo' })
1865+
);
18621866
});
18631867

18641868
test('redirect', async ({ page }) => {

packages/kit/types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ export interface Page<Params extends Record<string, string> = Record<string, str
302302
status: number;
303303
error: App.Error | null;
304304
data: App.PageData & Record<string, any>;
305+
form: any;
305306
}
306307

307308
export interface ParamMatcher {

0 commit comments

Comments
 (0)