Skip to content

Commit

Permalink
fix parsing endpoint json body (#1272)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hgl authored May 2, 2021
1 parent b4d0d6c commit f3ef93d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-bikes-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Not calling JSON.stringify on endpoint's body if it's a string and the content-type header denotes json
4 changes: 2 additions & 2 deletions packages/kit/src/runtime/server/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export default async function render_route(request, route) {
headers = lowercase_keys(headers);

if (
(typeof body === 'object' && !('content-type' in headers)) ||
headers['content-type'] === 'application/json'
typeof body === 'object' &&
(!('content-type' in headers) || headers['content-type'] === 'application/json')
) {
headers = { ...headers, 'content-type': 'application/json' };
body = JSON.stringify(body);
Expand Down
4 changes: 4 additions & 0 deletions packages/kit/test/apps/basics/src/routes/load/__tests__.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export default function (test, is_dev) {
);
});

test('json string is returned', '/load/relay', async ({ page }) => {
assert.equal(await page.textContent('h1'), '42');
});

test('prefers static data over endpoint', '/load/foo', async ({ page }) => {
assert.equal(await page.textContent('h1'), 'static file');
});
Expand Down
8 changes: 8 additions & 0 deletions packages/kit/test/apps/basics/src/routes/load/relay.json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function get() {
return {
headers: {
'content-type': 'application/json'
},
body: '42'
};
}
17 changes: 17 additions & 0 deletions packages/kit/test/apps/basics/src/routes/load/relay.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script context="module">
/** @type {import('@sveltejs/kit').Load} */
export async function load({ fetch }) {
const res = await fetch('/load/relay.json');
return {
props: {
answer: await res.json()
}
}
}
</script>

<script>
export let answer;
</script>

<h1>{answer}</h1>
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
export let answer;
</script>

<h1>{answer}</h1>
<h1>{answer}</h1>

0 comments on commit f3ef93d

Please sign in to comment.