Skip to content

Commit f3ef93d

Browse files
authored
fix parsing endpoint json body (#1272)
* fix parsing endpoint json body Converting to JSON string also when the 'content-type' header is 'application/json' prevents the endpoint from functioning as a relay, that is, it can't fetch an API that returns a JSON string and then forwards the headers and the body as they are, because svelte kit would call JSON.stringify on the string again. * Do call JSON.stringify if the body is not string and header denotes json
1 parent b4d0d6c commit f3ef93d

File tree

6 files changed

+37
-3
lines changed

6 files changed

+37
-3
lines changed

.changeset/real-bikes-matter.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+
Not calling JSON.stringify on endpoint's body if it's a string and the content-type header denotes json

packages/kit/src/runtime/server/endpoint.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export default async function render_route(request, route) {
3232
headers = lowercase_keys(headers);
3333

3434
if (
35-
(typeof body === 'object' && !('content-type' in headers)) ||
36-
headers['content-type'] === 'application/json'
35+
typeof body === 'object' &&
36+
(!('content-type' in headers) || headers['content-type'] === 'application/json')
3737
) {
3838
headers = { ...headers, 'content-type': 'application/json' };
3939
body = JSON.stringify(body);

packages/kit/test/apps/basics/src/routes/load/__tests__.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export default function (test, is_dev) {
2929
);
3030
});
3131

32+
test('json string is returned', '/load/relay', async ({ page }) => {
33+
assert.equal(await page.textContent('h1'), '42');
34+
});
35+
3236
test('prefers static data over endpoint', '/load/foo', async ({ page }) => {
3337
assert.equal(await page.textContent('h1'), 'static file');
3438
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function get() {
2+
return {
3+
headers: {
4+
'content-type': 'application/json'
5+
},
6+
body: '42'
7+
};
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script context="module">
2+
/** @type {import('@sveltejs/kit').Load} */
3+
export async function load({ fetch }) {
4+
const res = await fetch('/load/relay.json');
5+
return {
6+
props: {
7+
answer: await res.json()
8+
}
9+
}
10+
}
11+
</script>
12+
13+
<script>
14+
export let answer;
15+
</script>
16+
17+
<h1>{answer}</h1>

packages/kit/test/apps/basics/src/routes/load/serialization.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
export let answer;
1212
</script>
1313

14-
<h1>{answer}</h1>
14+
<h1>{answer}</h1>

0 commit comments

Comments
 (0)