Skip to content

fix: use window.fetch in load functions to allow libraries to patch it #10009

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

Merged
merged 8 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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/sharp-birds-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: Use `window.fetch` instead of unpatched fetch in `load` functions
31 changes: 27 additions & 4 deletions packages/kit/src/runtime/client/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ if (DEV) {

check_stack_trace();

/**
*
* @param {RequestInfo | URL} input
* @param {RequestInit & Record<string, any> | undefined} init
*/
window.fetch = (input, init) => {
// Check if fetch was called via load_node. the lock method only checks if it was called at the
// same time, but not necessarily if it was called from `load`.
Expand All @@ -36,10 +41,14 @@ if (DEV) {
const cutoff = stack_array.findIndex((a) => a.includes('load@') || a.includes('at load'));
const stack = stack_array.slice(0, cutoff + 2).join('\n');

const heuristic = can_inspect_stack_trace
const inLoadHeuristic = can_inspect_stack_trace
? stack.includes('src/runtime/client/client.js')
: loading;
if (heuristic) {

// This flag is set in initial_fetch and subsequent_fetch
const calledViaSvelteKitFetch = init?.__sveltekit_fetch__;

if (inLoadHeuristic && !calledViaSvelteKitFetch) {
console.warn(
`Loading ${url} using \`window.fetch\`. For best results, use the \`fetch\` that is passed to your \`load\` function: https://kit.svelte.dev/docs/load#making-fetch-requests`
);
Expand Down Expand Up @@ -86,7 +95,14 @@ export function initial_fetch(resource, opts) {
return Promise.resolve(new Response(body, init));
}

return native_fetch(resource, opts);
const patchedOpts = DEV
? {
...opts,
__sveltekit_fetch__: true
}
: opts;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably, this isn't super nice but it allows us to let the patched window.fetch know that it was called via event.fetch, therefore allowing us to always call window.fetch.

return window.fetch(resource, patchedOpts);
}

/**
Expand All @@ -112,7 +128,14 @@ export function subsequent_fetch(resource, resolved, opts) {
}
}

return native_fetch(resolved, opts);
const patchedOpts = DEV
? {
...opts,
__sveltekit_fetch__: true
}
: opts;

return window.fetch(resolved, patchedOpts);
}

/**
Expand Down