From 570562b74d9e9f295d9b617478088a650f51e96b Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Tue, 19 Nov 2024 11:45:57 +0800 Subject: [PATCH] fix: handle empty Headers when serialising Request passed to fetch (#13023) --- .changeset/honest-hornets-doubt.md | 5 +++++ packages/kit/src/runtime/client/client.js | 4 +++- .../load/fetch-request-empty-headers/+page.js | 6 ++++++ .../load/fetch-request-empty-headers/+page.svelte | 6 ++++++ packages/kit/test/apps/basics/test/client.test.js | 15 +++++++++++++++ 5 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 .changeset/honest-hornets-doubt.md create mode 100644 packages/kit/test/apps/basics/src/routes/load/fetch-request-empty-headers/+page.js create mode 100644 packages/kit/test/apps/basics/src/routes/load/fetch-request-empty-headers/+page.svelte diff --git a/.changeset/honest-hornets-doubt.md b/.changeset/honest-hornets-doubt.md new file mode 100644 index 000000000000..b34bed919926 --- /dev/null +++ b/.changeset/honest-hornets-doubt.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: prevent duplicate fetch request when using Request with load function's fetch diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index df35010c65c1..656174365198 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -669,7 +669,9 @@ async function load_node({ loader, parent, url, params, route, server_data_node : await resource.blob(), cache: resource.cache, credentials: resource.credentials, - headers: resource.headers, + // the headers are undefined on the server if the Headers object is empty + // so we need to make sure they are also undefined here if there are no headers + headers: [...resource.headers].length ? resource.headers : undefined, integrity: resource.integrity, keepalive: resource.keepalive, method: resource.method, diff --git a/packages/kit/test/apps/basics/src/routes/load/fetch-request-empty-headers/+page.js b/packages/kit/test/apps/basics/src/routes/load/fetch-request-empty-headers/+page.js new file mode 100644 index 000000000000..895e97248da7 --- /dev/null +++ b/packages/kit/test/apps/basics/src/routes/load/fetch-request-empty-headers/+page.js @@ -0,0 +1,6 @@ +/** @type {import('@sveltejs/kit').Load} */ +export async function load({ url, fetch }) { + const res = await fetch(new Request(url.origin + '/load/fetch-request.json')); + const { answer } = await res.json(); + return { answer }; +} diff --git a/packages/kit/test/apps/basics/src/routes/load/fetch-request-empty-headers/+page.svelte b/packages/kit/test/apps/basics/src/routes/load/fetch-request-empty-headers/+page.svelte new file mode 100644 index 000000000000..c890e7840e38 --- /dev/null +++ b/packages/kit/test/apps/basics/src/routes/load/fetch-request-empty-headers/+page.svelte @@ -0,0 +1,6 @@ + + +

the answer is {data.answer}

diff --git a/packages/kit/test/apps/basics/test/client.test.js b/packages/kit/test/apps/basics/test/client.test.js index 351cbdbeb7ad..56b2c96bc744 100644 --- a/packages/kit/test/apps/basics/test/client.test.js +++ b/packages/kit/test/apps/basics/test/client.test.js @@ -252,6 +252,21 @@ test.describe('Load', () => { expect(logs).toContain('Called a patched window.fetch'); }); + test('does not repeat fetch on hydration when using Request object', async ({ page }) => { + const requests = []; + page.on('request', (request) => { + if (request.url().includes('/load/fetch-request.json')) { + requests.push(request); + } + }); + + await page.goto('/load/fetch-request-empty-headers'); + + console.log({ requests }); + + expect(requests).toEqual([]); + }); + if (process.env.DEV) { test('using window.fetch causes a warning', async ({ page, baseURL }) => { await Promise.all([