Skip to content
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

fix: use window.fetch in load functions to allow libraries to patch it #10009

Merged
merged 8 commits into from
Oct 10, 2023

Conversation

Lms24
Copy link
Contributor

@Lms24 Lms24 commented May 22, 2023

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 getsentry/sentry-javascript#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:

  • 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
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • 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: getsentry/sentry-javascript#8174
(ref: https://github.com/Lms24/gh-js-8174)

@changeset-bot
Copy link

changeset-bot bot commented May 22, 2023

🦋 Changeset detected

Latest commit: b1420c5

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

Comment on lines 98 to 104
const patchedOpts = DEV
? {
...opts,
__sveltekit_fetch__: true
}
: opts;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably, this isn't super nice but it allows us to let the patched window.fetch know that it was called via event.fetch, therefore allowing us to always call window.fetch.

@Lms24 Lms24 marked this pull request as ready for review May 23, 2023 08:56
@Lms24
Copy link
Contributor Author

Lms24 commented Jun 27, 2023

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

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

native_fetch still exists/is used in a few other places - is that deliberate?

@eltigerchino
Copy link
Member

eltigerchino commented Sep 7, 2023

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.

@pzuraq
Copy link
Contributor

pzuraq commented Sep 7, 2023

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

@benmccann benmccann changed the title fix: Use window.fetch instead of unpatchable fetch in load functions fix: use window.fetch in load functions to allow libraries to patch it Oct 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants