Skip to content

Commit f532ccb

Browse files
committed
always use window.fetch, pass flag to patched window.fetch
1 parent ae835cf commit f532ccb

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

packages/kit/src/runtime/client/fetcher.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ if (DEV) {
2323

2424
check_stack_trace();
2525

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

39-
const heuristic = can_inspect_stack_trace
45+
const inLoadHeuristic = can_inspect_stack_trace
4046
? stack.includes('src/runtime/client/client.js')
4147
: loading;
42-
if (heuristic) {
48+
49+
// This flag is set in initial_fetch and subsequent_fetch
50+
const calledViaSvelteKitFetch = init?.__sveltekit_fetch__;
51+
52+
if (inLoadHeuristic && !calledViaSvelteKitFetch) {
4353
console.warn(
4454
`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`
4555
);
@@ -86,7 +96,14 @@ export function initial_fetch(resource, opts) {
8696
return Promise.resolve(new Response(body, init));
8797
}
8898

89-
return DEV ? native_fetch(resource, opts) : window.fetch(resource, opts);
99+
const patchedOpts = DEV
100+
? {
101+
...opts,
102+
__sveltekit_fetch__: true
103+
}
104+
: opts;
105+
106+
return window.fetch(resource, patchedOpts);
90107
}
91108

92109
/**
@@ -112,7 +129,14 @@ export function subsequent_fetch(resource, resolved, opts) {
112129
}
113130
}
114131

115-
return DEV ? native_fetch(resolved, opts) : window.fetch(resolved, opts);
132+
const patchedOpts = DEV
133+
? {
134+
...opts,
135+
__sveltekit_fetch__: true
136+
}
137+
: opts;
138+
139+
return window.fetch(resolved, patchedOpts);
116140
}
117141

118142
/**

0 commit comments

Comments
 (0)