Skip to content

Commit 5ef7d0b

Browse files
codercmsghostdevv
andauthored
fix: fetch doesn't respect headers from 1st arg if its type is Request (#10136)
Co-authored-by: Willow (GHOST) <[email protected]>
1 parent 5ff4f91 commit 5ef7d0b

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

.changeset/tricky-bikes-tan.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+
fix: Fetch doesn't respect headers from 1st arg if its type is Request

packages/kit/src/runtime/server/page/load_data.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ export function create_universal_fetch(event, state, fetched, csr, resolve_opts)
203203
*/
204204
return async (input, init) => {
205205
const cloned_body = input instanceof Request && input.body ? input.clone().body : null;
206+
207+
const cloned_headers =
208+
input instanceof Request && [...input.headers].length
209+
? new Headers(input.headers)
210+
: init?.headers;
211+
206212
let response = await event.fetch(input, init);
207213

208214
const url = new URL(input instanceof Request ? input.url : input, event.url);
@@ -260,7 +266,7 @@ export function create_universal_fetch(event, state, fetched, csr, resolve_opts)
260266
? await stream_to_string(cloned_body)
261267
: init?.body
262268
),
263-
request_headers: init?.headers,
269+
request_headers: cloned_headers,
264270
response_body: body,
265271
response
266272
});

packages/kit/test/apps/basics/test/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,10 @@ test.describe('Load', () => {
264264
const payload_b = '{"status":200,"statusText":"","headers":{},"body":"Y"}';
265265
// by the time JS has run, hydration will have nuked these scripts
266266
const script_contents_a = await page.innerHTML(
267-
'script[data-sveltekit-fetched][data-url="/load/serialization-post.json"][data-hash="3t25"]'
267+
'script[data-sveltekit-fetched][data-url="/load/serialization-post.json"][data-hash="1vn6nlx"]'
268268
);
269269
const script_contents_b = await page.innerHTML(
270-
'script[data-sveltekit-fetched][data-url="/load/serialization-post.json"][data-hash="3t24"]'
270+
'script[data-sveltekit-fetched][data-url="/load/serialization-post.json"][data-hash="1vn6nlw"]'
271271
);
272272

273273
expect(script_contents_a).toBe(payload_a);

0 commit comments

Comments
 (0)