From c4137536f2c6572eaeec1a82ccea0852f5be6b98 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Wed, 11 Jan 2023 17:24:12 +0100 Subject: [PATCH] [fix] no false positive warnings for fetch uses in firefox (#8456) fixes #7992 --- .changeset/shy-readers-help.md | 5 +++++ packages/kit/src/runtime/client/fetcher.js | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 .changeset/shy-readers-help.md diff --git a/.changeset/shy-readers-help.md b/.changeset/shy-readers-help.md new file mode 100644 index 000000000000..df5205f0df09 --- /dev/null +++ b/.changeset/shy-readers-help.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +[fix] no false positive warnings for fetch uses in firefox diff --git a/packages/kit/src/runtime/client/fetcher.js b/packages/kit/src/runtime/client/fetcher.js index 36666ef29c41..90db3fe4456c 100644 --- a/packages/kit/src/runtime/client/fetcher.js +++ b/packages/kit/src/runtime/client/fetcher.js @@ -24,12 +24,20 @@ if (DEV) { check_stack_trace(); 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`. + // We use just the filename as the method name sometimes does not appear on the CI. const url = input instanceof Request ? input.url : input.toString(); - const stack = /** @type {string} */ (new Error().stack); + const stack_array = /** @type {string} */ (new Error().stack).split('\n'); + // We need to do some Firefox-specific cutoff because it (impressively) maintains the stack + // across events and for example traces a `fetch` call triggered from a button back + // to the creation of the event listener and the element creation itself, + // where at some point client.js will show up, leading to false positives. + const firefox_cutoff = stack_array.findIndex((a) => a.includes('*listen@')); + const stack = stack_array + .slice(0, firefox_cutoff !== -1 ? firefox_cutoff : undefined) + .join('\n'); - // 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` - // we use just the filename as the method name sometimes does not appear on the CI const heuristic = can_inspect_stack_trace ? stack.includes('src/runtime/client/client.js') : loading;