File tree 4 files changed +44
-7
lines changed
src/routes/load/window-fetch/outside-load
4 files changed +44
-7
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @sveltejs/kit ' : patch
3
+ ---
4
+
5
+ fix: prevent false positive warnings for fetch in Firefox and Safari
Original file line number Diff line number Diff line change @@ -29,14 +29,12 @@ if (DEV) {
29
29
// We use just the filename as the method name sometimes does not appear on the CI.
30
30
const url = input instanceof Request ? input . url : input . toString ( ) ;
31
31
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,
35
35
// 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' ) ;
40
38
41
39
const heuristic = can_inspect_stack_trace
42
40
? stack . includes ( 'src/runtime/client/client.js' )
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change @@ -754,3 +754,24 @@ test.describe('Interactivity', () => {
754
754
expect ( errored ) . toBe ( false ) ;
755
755
} ) ;
756
756
} ) ;
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments