Skip to content

Commit b451705

Browse files
authored
fix: correctly access app.decoders in inline and single output apps (#13871)
1 parent 73cc8a5 commit b451705

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

.changeset/stupid-singers-talk.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+
fix: correctly access transport decoders on the client when building for a single or inline output app

packages/kit/src/runtime/app/forms.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { app as server_app } from '../server/app.js';
66

77
export { applyAction };
88

9-
const decoders = BROWSER ? client_app.decoders : server_app?.decoders;
10-
119
/**
1210
* Use this function to deserialize the response from a form submission.
1311
* Usage:
@@ -34,7 +32,9 @@ export function deserialize(result) {
3432
const parsed = JSON.parse(result);
3533

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

4040
return parsed;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let data;
3+
</script>
4+
5+
<p>{data.data.text}</p>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { deserialize } from '$app/forms';
2+
3+
export const ssr = false;
4+
5+
export function load() {
6+
const result = deserialize(
7+
JSON.stringify({
8+
type: 'success',
9+
status: 200,
10+
data: '[{"text":1},"Hello world!"]'
11+
})
12+
);
13+
14+
return result;
15+
}

packages/kit/test/apps/options-2/test/test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ test.describe('Service worker', () => {
108108
});
109109

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

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

121121
test.describe("bundleStrategy: 'single'", () => {
122122
test.skip(({ javaScriptEnabled }) => !javaScriptEnabled || !!process.env.DEV);
123+
123124
test('loads a single js file and a single css file', async ({ page }) => {
124125
/** @type {string[]} */
125126
const requests = [];
@@ -135,4 +136,9 @@ test.describe("bundleStrategy: 'single'", () => {
135136
expect(requests.filter((req) => req.endsWith('.js')).length).toBe(1);
136137
expect(requests.filter((req) => req.endsWith('.css')).length).toBe(1);
137138
});
139+
140+
test('app.decoders is accessed only after app has been initialised', async ({ page }) => {
141+
await page.goto('/basepath/deserialize');
142+
await expect(page.locator('p')).toHaveText('Hello world!');
143+
});
138144
});

0 commit comments

Comments
 (0)