Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle empty Headers when serialising Request passed to fetch #13023

Merged
merged 6 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
benmccann marked this conversation as resolved.
Show resolved Hide resolved
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 @@
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 Expand Up @@ -954,7 +969,7 @@
expect(page.locator('p.loadingfail')).toBeHidden();
});

test('Catches fetch errors from server load functions (direct hit)', async ({ page }) => {

Check warning on line 972 in packages/kit/test/apps/basics/test/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit (20, ubuntu-latest, chromium)

flaky test: Catches fetch errors from server load functions (direct hit)

retries: 2

Check warning on line 972 in packages/kit/test/apps/basics/test/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit (22, ubuntu-latest, chromium)

flaky test: Catches fetch errors from server load functions (direct hit)

retries: 2
page.goto('/streaming/server-error');
await expect(page.locator('p.eager')).toHaveText('eager');
await expect(page.locator('p.fail')).toHaveText('fail');
Expand Down
Loading