kit icon indicating copy to clipboard operation
kit copied to clipboard

fix: Use `window.fetch` instead of unpatchable fetch in `load` functions

Open Lms24 opened this issue 2 years ago • 1 comments

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.fetch it in DEV mode (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 test and lint the project with pnpm lint and pnpm 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 changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

ref: #9530 ref: https://github.com/getsentry/sentry-javascript/issues/8174 (ref: https://github.com/Lms24/gh-js-8174)

Lms24 avatar May 22 '23 15:05 Lms24

🦋 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

changeset-bot[bot] avatar May 22 '23 15:05 changeset-bot[bot]

@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 :)

Lms24 avatar Jun 27 '23 08:06 Lms24

native_fetch still 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.

teemingc avatar Sep 07 '23 09:09 teemingc

Just wanted to say, big +1 to this PR, we could really use this in our app!

pzuraq avatar Sep 07 '23 11:09 pzuraq