From f3ef93d16eed098fc2c247279e44e560bf46f5fe Mon Sep 17 00:00:00 2001 From: Glen Huang Date: Sun, 2 May 2021 23:39:37 +0800 Subject: [PATCH] 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 --- .changeset/real-bikes-matter.md | 5 +++++ packages/kit/src/runtime/server/endpoint.js | 4 ++-- .../apps/basics/src/routes/load/__tests__.js | 4 ++++ .../apps/basics/src/routes/load/relay.json.js | 8 ++++++++ .../apps/basics/src/routes/load/relay.svelte | 17 +++++++++++++++++ .../basics/src/routes/load/serialization.svelte | 2 +- 6 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 .changeset/real-bikes-matter.md create mode 100644 packages/kit/test/apps/basics/src/routes/load/relay.json.js create mode 100644 packages/kit/test/apps/basics/src/routes/load/relay.svelte diff --git a/.changeset/real-bikes-matter.md b/.changeset/real-bikes-matter.md new file mode 100644 index 000000000000..0ec2d6d1d7ba --- /dev/null +++ b/.changeset/real-bikes-matter.md @@ -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 diff --git a/packages/kit/src/runtime/server/endpoint.js b/packages/kit/src/runtime/server/endpoint.js index cb0293eb2c43..ea89baf4d32c 100644 --- a/packages/kit/src/runtime/server/endpoint.js +++ b/packages/kit/src/runtime/server/endpoint.js @@ -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); diff --git a/packages/kit/test/apps/basics/src/routes/load/__tests__.js b/packages/kit/test/apps/basics/src/routes/load/__tests__.js index dce105f8f525..987451146254 100644 --- a/packages/kit/test/apps/basics/src/routes/load/__tests__.js +++ b/packages/kit/test/apps/basics/src/routes/load/__tests__.js @@ -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'); }); diff --git a/packages/kit/test/apps/basics/src/routes/load/relay.json.js b/packages/kit/test/apps/basics/src/routes/load/relay.json.js new file mode 100644 index 000000000000..7213f8048d45 --- /dev/null +++ b/packages/kit/test/apps/basics/src/routes/load/relay.json.js @@ -0,0 +1,8 @@ +export function get() { + return { + headers: { + 'content-type': 'application/json' + }, + body: '42' + }; +} diff --git a/packages/kit/test/apps/basics/src/routes/load/relay.svelte b/packages/kit/test/apps/basics/src/routes/load/relay.svelte new file mode 100644 index 000000000000..80d3acb6ed3c --- /dev/null +++ b/packages/kit/test/apps/basics/src/routes/load/relay.svelte @@ -0,0 +1,17 @@ + + + + +

{answer}

diff --git a/packages/kit/test/apps/basics/src/routes/load/serialization.svelte b/packages/kit/test/apps/basics/src/routes/load/serialization.svelte index f55089edb379..ff7a700621e1 100644 --- a/packages/kit/test/apps/basics/src/routes/load/serialization.svelte +++ b/packages/kit/test/apps/basics/src/routes/load/serialization.svelte @@ -11,4 +11,4 @@ export let answer; -

{answer}

\ No newline at end of file +

{answer}