-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't warn about
window.fetch
during hydration (#5041)
* failing test for #5031 * move fetch logic into separate module - fixes #5031 * changeset * remove .only
- Loading branch information
1 parent
d5cf5e0
commit 948c980
Showing
7 changed files
with
105 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/kit': patch | ||
--- | ||
|
||
don't warn about window.fetch during hydration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { hash } from '../hash.js'; | ||
|
||
let loading = 0; | ||
|
||
export const native = window.fetch; | ||
|
||
export function increment() { | ||
loading += 1; | ||
} | ||
|
||
export function decrement() { | ||
loading -= 1; | ||
} | ||
|
||
if (import.meta.env.DEV) { | ||
let can_inspect_stack_trace = false; | ||
|
||
const check_stack_trace = async () => { | ||
const stack = /** @type {string} */ (new Error().stack); | ||
can_inspect_stack_trace = stack.includes('check_stack_trace'); | ||
}; | ||
|
||
check_stack_trace(); | ||
|
||
window.fetch = (input, init) => { | ||
const url = input instanceof Request ? input.url : input.toString(); | ||
const stack = /** @type {string} */ (new Error().stack); | ||
|
||
const heuristic = can_inspect_stack_trace ? stack.includes('load_node') : loading; | ||
if (heuristic) { | ||
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/loading#input-fetch` | ||
); | ||
} | ||
return native(input, init); | ||
}; | ||
} | ||
|
||
/** | ||
* @param {RequestInfo} resource | ||
* @param {RequestInit} [opts] | ||
*/ | ||
export function initial(resource, opts) { | ||
const url = JSON.stringify(typeof resource === 'string' ? resource : resource.url); | ||
|
||
let selector = `script[sveltekit\\:data-type="data"][sveltekit\\:data-url=${url}]`; | ||
|
||
if (opts && typeof opts.body === 'string') { | ||
selector += `[sveltekit\\:data-body="${hash(opts.body)}"]`; | ||
} | ||
|
||
const script = document.querySelector(selector); | ||
if (script && script.textContent) { | ||
const { body, ...init } = JSON.parse(script.textContent); | ||
return Promise.resolve(new Response(body, init)); | ||
} | ||
|
||
return native(resource, opts); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/kit/test/apps/basics/src/routes/load/window-fetch/correct.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<script context="module"> | ||
/** @type {import('./index').Load} */ | ||
export async function load({ url, fetch }) { | ||
const res = await fetch(`${url.origin}/load/window-fetch/data.json`); | ||
const { answer } = await res.json(); | ||
return { | ||
props: { answer } | ||
}; | ||
} | ||
</script> | ||
|
||
<script> | ||
/** @type {number} */ | ||
export let answer; | ||
</script> | ||
|
||
<h1>{answer}</h1> |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters