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

[Feature]: Expose capacitor fetch without clobbering window.fetch #7746

Closed
1 of 3 tasks
aeharding opened this issue Nov 3, 2024 · 2 comments
Closed
1 of 3 tasks

[Feature]: Expose capacitor fetch without clobbering window.fetch #7746

aeharding opened this issue Nov 3, 2024 · 2 comments
Labels

Comments

@aeharding
Copy link

aeharding commented Nov 3, 2024

Description

Expose Capacitor fetch-compatible shim without overriding window.fetch

Platforms

  • iOS
  • Android
  • Web

Request or proposed solution

Right now, the only way to allow my libs to use Capacitor's fetch is enabling a native shim that clobbers fetch on the window. This can cause issues because the developer cannot control which libraries use the capacitor fetch shim, vs browser fetch.

I would like to see capactor's fetch shim exposed on window.Capacitor.fetch (or similar) so that the developer has more fine grained control over calling Capacitor's fetch, vs browser fetch.

Alternatives

A hacky workaround is saving native window.fetch, waiting for Capacitor to setup, and then saving Capacitor's window.fetch and resetting the original browser fetch. But its hacky and doesn't account for side effects like patching XHR.

Another alternative is using CapacitorHttp directly and building (and maintaining) custom shims. However this can be cumbersome to maintain and account for all the intricacies of fetching (also, some libs only allow passing a custom fetch and don't support custom adapters, like https://github.com/LemmyNet/lemmy-js-client).

Additional Information

I've experienced many bugs by enabling capacitor's clobbering of fetch and XMLHttpRequest on window, so I've turned it off.

However, I still want to access to the Capacitor fetch shim (without clobbering window.fetch) so I can use it when I want. So, in my project, I have to copy Capacitor's implementation to my codebase. This is not ideal because I have to keep it up to date:

https://github.com/aeharding/voyager/blob/2725f478526f0dab7827c303f4f8922ad377a3be/src/services/nativeFetch.ts#L13

@jcesarmobile
Copy link
Member

The original fetch is saved into CapacitorWebFetch before patching, you can swap them if you want.

@jcesarmobile jcesarmobile closed this as not planned Won't fix, can't repro, duplicate, stale Nov 28, 2024
Copy link

ionitron-bot bot commented Dec 28, 2024

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Dec 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants