Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly deserialize on the server #13686

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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/silly-berries-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correctly run `deserialize` on the server
3 changes: 2 additions & 1 deletion packages/kit/src/runtime/app/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export function deserialize(result) {
const parsed = JSON.parse(result);

if (parsed.data) {
parsed.data = devalue.parse(parsed.data, app.decoders);
// app is uninitialised on the server
parsed.data = devalue.parse(parsed.data, app?.decoders);
}

return parsed;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const actions = {
default: () => {
return { a: 1 };
}
};
13 changes: 13 additions & 0 deletions packages/kit/test/apps/basics/src/routes/deserialize/+server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { deserialize } from '$app/forms';

export async function GET({ fetch }) {
const response = await fetch('/deserialize', {
method: 'POST',
body: new FormData(),
headers: {
'x-sveltekit-action': 'true'
}
});
const result = deserialize(await response.text());
return Response.json(result);
}
7 changes: 7 additions & 0 deletions packages/kit/test/apps/basics/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,3 +716,10 @@ test.describe('getRequestEvent', () => {
expect(await response.text()).toBe('hello from hooks.server.js');
});
});

test.describe('$app/forms', () => {
test('deserialize works on the server', async ({ request }) => {
const response = await request.get('/deserialize');
expect(await response.json()).toEqual({ type: 'success', status: 200, data: { a: 1 } });
});
});
Loading