Skip to content

Commit 95e9582

Browse files
authored
fix: prevent false positive warnings for fetch in Firefox and Safari (#9680)
* change fetch warning stack trace cutoff * changeset * add test
1 parent 563e3e3 commit 95e9582

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

.changeset/flat-trainers-fold.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: prevent false positive warnings for fetch in Firefox and Safari

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@ if (DEV) {
2929
// We use just the filename as the method name sometimes does not appear on the CI.
3030
const url = input instanceof Request ? input.url : input.toString();
3131
const stack_array = /** @type {string} */ (new Error().stack).split('\n');
32-
// We need to do some Firefox-specific cutoff because it (impressively) maintains the stack
33-
// across events and for example traces a `fetch` call triggered from a button back
34-
// to the creation of the event listener and the element creation itself,
32+
// We need to do a cutoff because Safari and Firefox maintain the stack
33+
// across events and for example traces a `fetch` call triggered from a button
34+
// back to the creation of the event listener and the element creation itself,
3535
// where at some point client.js will show up, leading to false positives.
36-
const firefox_cutoff = stack_array.findIndex((a) => a.includes('*listen@'));
37-
const stack = stack_array
38-
.slice(0, firefox_cutoff !== -1 ? firefox_cutoff : undefined)
39-
.join('\n');
36+
const cutoff = stack_array.findIndex((a) => a.includes('load@') || a.includes('at load'));
37+
const stack = stack_array.slice(0, cutoff + 2).join('\n');
4038

4139
const heuristic = can_inspect_stack_trace
4240
? stack.includes('src/runtime/client/client.js')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
import { page } from '$app/stores';
3+
import { onMount } from 'svelte';
4+
5+
let answer = 0;
6+
7+
onMount(async () => {
8+
const res = await fetch(`${$page.url.origin}/load/window-fetch/data.json`);
9+
({ answer } = await res.json());
10+
});
11+
</script>
12+
13+
<h1>{answer}</h1>

packages/kit/test/apps/basics/test/cross-platform/client.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -754,3 +754,24 @@ test.describe('Interactivity', () => {
754754
expect(errored).toBe(false);
755755
});
756756
});
757+
758+
test.describe('Load', () => {
759+
if (process.env.DEV) {
760+
test('using window.fetch does not cause false-positive warning', async ({ page, baseURL }) => {
761+
/** @type {string[]} */
762+
const warnings = [];
763+
page.on('console', (msg) => {
764+
if (msg.type() === 'warning') {
765+
warnings.push(msg.text());
766+
}
767+
});
768+
769+
await page.goto('/load/window-fetch/outside-load');
770+
expect(await page.textContent('h1')).toBe('42');
771+
772+
expect(warnings).not.toContain(
773+
`Loading ${baseURL}/load/window-fetch/data.json 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`
774+
);
775+
});
776+
}
777+
});

0 commit comments

Comments
 (0)