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 @@ + + +