Skip to content

Commit

Permalink
fix: handle empty Headers when serialising Request passed to fetch (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
eltigerchino authored Nov 19, 2024
1 parent 143dbf9 commit 570562b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/honest-hornets-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: prevent duplicate fetch request when using Request with load function's fetch
4 changes: 3 additions & 1 deletion packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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 };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
/** @type {import('./$types').PageData} */
export let data;
</script>

<h1>the answer is {data.answer}</h1>
15 changes: 15 additions & 0 deletions packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit 570562b

Please sign in to comment.