fix: Use `window.fetch` instead of unpatchable fetch in `load` functions
This PR makes initial_fetch and subsequent_fetch use the patched window.fetch function instead of the unpatched and stored away native_fetch function.
This change brings one big benefit: Additional behaviour added by libraries (like Sentry but there are many others) that patch window.fetch is now also invoked in event.fetch calls within client-side universal load functions.
Additionally, this provides a way for libraries to instrument fetch in load functions without causing unintended cache misses (see https://github.com/getsentry/sentry-javascript/issues/8174)
Kit patches window.fetch for two reasons:
- to warn about directly calling
window.fetchit inDEVmode (this we should leave as-is) - to clean the cache
I'm not sure if I'm missing something but cleaning the cache should be alright in load functions as well 🤔
Getting this PR merged would de-prioritize needing a dedicated handleFetch hook on the client-side (#9530) for us (which might still be worthwhile but for other reasons).
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- [x] This message body should clearly illustrate what problems it solves.
- [x] Ideally, include a test that fails without this PR but passes with it.
Tests
- [x] Run the tests with
pnpm testand lint the project withpnpm lintandpnpm check
Changesets
- [x] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.
ref: #9530 ref: https://github.com/getsentry/sentry-javascript/issues/8174 (ref: https://github.com/Lms24/gh-js-8174)
🦋 Changeset detected
Latest commit: b1420c5fb5353d2ea64282c6bbd4238d046b5c30
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
| Name | Type |
|---|---|
| @sveltejs/kit | Patch |
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR
@benmccann thanks for the review and sorry for missing out on the code styling. I made an additional small change (see comment above). Lmk If I can do anything else to help getting this through :)
native_fetchstill exists/is used in a few other places - is that deliberate?
I think it's still required for our own patches to window.fetch and when it's used by the client to retrieve load function data. If not, it would falsely fire off those "don't use window.fetch" warnings I think.
Just wanted to say, big +1 to this PR, we could really use this in our app!