-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: use window.fetch in load functions to allow libraries to patch it
#10009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b6e1fca
fix: Use `window.fetch` instead of unpatched fetch in `load` functions
Lms24 ae835cf
changeset
Lms24 4500e5b
always use window.fetch, pass flag to patched window.fetch
Lms24 0e1d0e0
add test
Lms24 9dfde88
apply code review suggestions
Lms24 45c93a4
make __sveltekit_fetch__ non-enumerable
Lms24 64e858d
Update .changeset/sharp-birds-invent.md
benmccann b1420c5
DRY
benmccann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||
| --- | ||
|
|
||
| fix: Use `window.fetch` instead of unpatched fetch in `load` functions | ||
This file contains hidden or 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
20 changes: 20 additions & 0 deletions
20
packages/kit/test/apps/basics/src/routes/load/window-fetch/patching/+page.js
This file contains hidden or 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,20 @@ | ||
| import { browser } from '$app/environment'; | ||
|
|
||
| /** @type {import('./$types').PageLoad} */ | ||
| export async function load({ url, fetch }) { | ||
| // simulate fetch being monkey-patched by a 3rd party library | ||
| // run everything only in browser to avoid SSR caching | ||
| if (browser) { | ||
| const original_fetch = window.fetch; | ||
| window.fetch = (input, init) => { | ||
| console.log('Called a patched window.fetch'); | ||
| return original_fetch(input, init); | ||
| }; | ||
|
|
||
| const res = await fetch(`${url.origin}/load/window-fetch/data.json`); | ||
| const { answer } = await res.json(); | ||
| window.fetch = original_fetch; | ||
| return { answer }; | ||
| } | ||
| return {}; | ||
| } |
6 changes: 6 additions & 0 deletions
6
packages/kit/test/apps/basics/src/routes/load/window-fetch/patching/+page.svelte
This file contains hidden or 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,6 @@ | ||
| <script> | ||
| /** @type {import('./$types').PageData} */ | ||
| export let data; | ||
| </script> | ||
|
|
||
| <h1>{data.answer}</h1> |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.