Skip to content

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

Merged
benmccann merged 8 commits into
sveltejs:masterfrom
Lms24:use-window-fetch
Oct 10, 2023
Merged

benmccann merged 8 commits into
sveltejs:masterfrom
Lms24:use-window-fetch

Conversation

@Lms24

@Lms24 Lms24 commented May 22, 2023 •

Copy link
Copy Markdown
Contributor

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:

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

changeset-bot Bot commented May 22, 2023 •

Copy link
Copy Markdown

🦋 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
Copy Markdown
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

Lms24 commented Jun 27, 2023

Copy link
Copy Markdown
Contributor Author

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

@dummdidumm dummdidumm left a comment

Copy link
Copy Markdown
Member

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?

@teemingc

teemingc commented Sep 7, 2023 •

Copy link
Copy Markdown
Member

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

pzuraq commented Sep 7, 2023

Copy link
Copy Markdown
Contributor

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

Comment thread .changeset/sharp-birds-invent.md Resolved
@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

This branch was successfully deployed

3 active (1 outdated) deployments
Preview – kit-svelte-dev — b1420c5f Deployed Oct 9, 2023 by vercel[bot]
Preview – kit-default-template — b1420c5f Deployed Oct 9, 2023 by vercel[bot]
Preview – kit — 0e1d0e04 Deployed May 23, 2023 by vercel[bot]
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