Skip to content

fix: correctly access app.decoders in inline and single output apps #13871

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

Merged
Merged
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/stupid-singers-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correctly access transport decoders on the client when building for a single or inline output app
6 changes: 3 additions & 3 deletions packages/kit/src/runtime/app/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { app as server_app } from '../server/app.js';

export { applyAction };

const decoders = BROWSER ? client_app.decoders : server_app?.decoders;

/**
* Use this function to deserialize the response from a form submission.
* Usage:
Expand All @@ -34,7 +32,9 @@ export function deserialize(result) {
const parsed = JSON.parse(result);

if (parsed.data) {
parsed.data = devalue.parse(parsed.data, decoders);
// the decoders should never be initialised at the top-level because `app`
// will not initialised yet if `kit.output.bundleStrategy` is 'single' or 'inline'
parsed.data = devalue.parse(parsed.data, BROWSER ? client_app.decoders : server_app.decoders);
}

return parsed;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let data;
</script>

<p>{data.data.text}</p>
15 changes: 15 additions & 0 deletions packages/kit/test/apps/options-2/src/routes/deserialize/+page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { deserialize } from '$app/forms';

export const ssr = false;

export function load() {
const result = deserialize(
JSON.stringify({
type: 'success',
status: 200,
data: '[{"text":1},"Hello world!"]'
})
);

return result;
}
8 changes: 7 additions & 1 deletion packages/kit/test/apps/options-2/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ test.describe('Service worker', () => {
});

expect(self.base).toBe('/basepath');
expect(self.build[0]).toMatch(/\/basepath\/_app\/immutable\/bundle\.[\w-]+\.js/);
expect(self.build?.[0]).toMatch(/\/basepath\/_app\/immutable\/bundle\.[\w-]+\.js/);
expect(self.image_src).toMatch(/\/basepath\/_app\/immutable\/assets\/image\.[\w-]+\.jpg/);
});

Expand All @@ -120,6 +120,7 @@ test.describe('Service worker', () => {

test.describe("bundleStrategy: 'single'", () => {
test.skip(({ javaScriptEnabled }) => !javaScriptEnabled || !!process.env.DEV);

test('loads a single js file and a single css file', async ({ page }) => {
/** @type {string[]} */
const requests = [];
Expand All @@ -135,4 +136,9 @@ test.describe("bundleStrategy: 'single'", () => {
expect(requests.filter((req) => req.endsWith('.js')).length).toBe(1);
expect(requests.filter((req) => req.endsWith('.css')).length).toBe(1);
});

test('app.decoders is accessed only after app has been initialised', async ({ page }) => {
await page.goto('/basepath/deserialize');
await expect(page.locator('p')).toHaveText('Hello world!');
});
});
Loading