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

Cross-fetch is not usable in service workers #78

Closed
pimterry opened this issue Oct 5, 2020 · 13 comments
Closed

Cross-fetch is not usable in service workers #78

pimterry opened this issue Oct 5, 2020 · 13 comments
Labels

Comments

@pimterry
Copy link

pimterry commented Oct 5, 2020

I'm using this library (via https://github.com/prisma-labs/graphql-request) and it fails to make requests when used within a browser service worker. More specifically, it throws:

ReferenceError: XMLHttpRequest is not defined
    at browser-ponyfill.js:463
    at new Promise (<anonymous>)
    at Object.fetch [as default] (browser-ponyfill.js:455)
    at GraphQLClient.<anonymous> (index.ts:87)
    at step (createRequestBody.ts:45)
    at Object.next (createRequestBody.ts:45)
    at createRequestBody.ts:45
    at new Promise (<anonymous>)
    at createRequestBody.ts:45
    at GraphQLClient.request (index.ts:73)

Service workers do always have fetch available, but they don't have XMLHttpRequest. See whatwg/xhr#19 for some background from the spec discussion.

Because they are a browser environment though, https://github.com/lquixada/cross-fetch/blob/master/dist/browser-ponyfill.js from this library is used, which incorrectly seems to assume XHR is always available in all modern browser environments.

I think all that's required here is to fall back to the built-in Fetch that is available in this case.

@madeleineostoja
Copy link

I've also hit this issue, but in a different context — Sapper also has fetch available in its SSR environment but not XMLHttpRequest, and one of my dependencies (prismic-javascript) uses cross-fetch, making it unusable.

@abnemo
Copy link

abnemo commented Apr 26, 2021

Hello guys!
I discovered a cool project called supabase. However, I wanted to use their javascript client supabase-js with deno, but XMLHttpRequest is not defined there too =(

stickfigure added a commit to stickfigure/hatteryjs that referenced this issue May 4, 2021
It creates an issue with service workers:
lquixada/cross-fetch#78

We can add it back when that's fixed.
@jcs224
Copy link

jcs224 commented May 11, 2021

Hopefully, this will be fixed now. A dependency for this library was just updated for this situation. JakeChampion/fetch#967

@abnemo
Copy link

abnemo commented May 17, 2021

Any plans for the next release with updated dependencies?

@Nick-Mazuk
Copy link

Running into this issue on Cloudflare Workers as well. Any dependency that uses cross-fetch cannot be used on Cloudflare Workers right now.

michaelsnook added a commit to michaelsnook/sunlo-remix-vercel that referenced this issue Dec 8, 2021
Supabase currently doesn't work on Cloudflare Workers because of
lquixada/cross-fetch#78
@ntinkler-calendly
Copy link

Can confirm that I also see this issue when using packages that depend on cross-fetch in chrome-extension background scripts (Manifest v3, service worker based).

Cross-fetch blows up trying to use XHR to implement fetch when the environment provides fetch but not XHR.

Example error

"ReferenceError: XMLHttpRequest is not defined
    at chrome-extension://dohbfkgabajhohdidhmhhpfagifoacnk/background.js:1716:23
    at new Promise (<anonymous>)
    at fetch (chrome-extension://dohbfkgabajhohdidhmhhpfagifoacnk/background.js:1709:18)

@arun3528
Copy link

Has anyone found any alternate library or make cross fetch work in service worker

@ntinkler-calendly
Copy link

Has anyone found any alternate library or make cross fetch work in service worker

Given that service workers are only present in browsers that provide fetch natively, I've begun patching packages locally (yarn patch - in my case) and just ripping cross-fetch out when I build for service worker contexts. It feels very silly that this library doesn't simply expose the native implementation when it's present, but they might have a reason for falling back to XHR.

Either way - given that there doesn't appear to be any effort to fix this here, and even if fixed, you'll have to wait for 3rd party libraries to pick up the update, removing it is the only sane path forward I found.

It's a pain in the butt, but given that most libraries import this using either

import fetch from 'cross-fetch'
// or
const fetch = require('cross-fetch')

Just deleting the line before bundling will cause later references to pick up the global native implementation and work as expected.

@arun3528
Copy link

arun3528 commented Feb 18, 2022 via email

@karol-cf
Copy link

Hi,

I have this issue when trying to use Supabase in an MV3 Chrome Extension's service workers. I'm using an example I've found in this YouTube video (he's using MV2, which utilizes a background page, instead of service workers, so the issue is not present there). The extension allows you to sign up and log in with Supabase in a new Chrome tab. I've got a bundled version of supabase-js in my project. The supabase-js library is imported in the service worker and I create a client (as you can see on the screenshot below, I've tried binding the fetch as well). Whenever the user wants to authenticate, I send a message through the message passing API to my service worker and call various supabase.auth methods. Every time the signUp method is called, for example, I get "ReferenceError: XMLHttpRequest is not defined" and the method does not proceed further (on the third screenshot I've tried signing up and it threw an error).

image
image (1)
image (2)

The only solution, that I think might work is removing the cross-fetch import from your lib and then bundling it, as mentioned above. Because of the MV3 requirements, I need that bundled js file anyway. However, I'd much rather not do that, because I don't think that this is a clean way of solving the problem. Any other ideas?

ThisIsMissEm added a commit to ThisIsMissEm/cross-fetch that referenced this issue May 26, 2022
I'm not sure why the code was written the way it is, but it appears to be forcing the polyfill to be used even in environments where the Fetch API exists natively, which isn't intentional and causes the bugs described in lquixada#91, lquixada#78, and lquixada#69.

The original commit and PR don't describe why the behaviour was changed: lquixada@123b1a2
bahlo pushed a commit to axiomhq/cross-fetch that referenced this issue Jun 3, 2022
I'm not sure why the code was written the way it is, but it appears to be forcing the polyfill to be used even in environments where the Fetch API exists natively, which isn't intentional and causes the bugs described in lquixada#91, lquixada#78, and lquixada#69.

The original commit and PR don't describe why the behaviour was changed: lquixada@123b1a2
bahlo pushed a commit to axiomhq/cross-fetch that referenced this issue Jun 3, 2022
I'm not sure why the code was written the way it is, but it appears to be forcing the polyfill to be used even in environments where the Fetch API exists natively, which isn't intentional and causes the bugs described in lquixada#91, lquixada#78, and lquixada#69.

The original commit and PR don't describe why the behaviour was changed: lquixada@123b1a2
@dax70
Copy link

dax70 commented Jan 17, 2023

Same issue with Cloudflare workers and noticed it's been open for 2+ years.

Any concerns in uncommenting line: 49?
https://github.com/lquixada/cross-fetch/blob/main/rollup.config.js#L49

var ctx = global.fetch ? global : __self__;

I am using a package that uses cross-fetch.

@lquixada
Copy link
Owner

lquixada commented Jun 9, 2023

I've been working on version 4 of cross-fetch to fix this issue. If anyone's interested, please run npm install cross-fetch@latest-v4.x in your project and give it a try. Let me know if any issues come up.

@lquixada
Copy link
Owner

lquixada commented Jul 3, 2023

Version 4 has been officially released with the fix. Please check it out: npm install cross-fetch.

@lquixada lquixada closed this as completed Jul 8, 2023
rdvorkin added a commit to rdvorkin/web3.js that referenced this issue Sep 27, 2023
Fixes issues like lquixada/cross-fetch#78, enabling to run web3.js in service worker
luu-alex pushed a commit to web3/web3.js that referenced this issue Oct 2, 2023
* Bump cross-fetch to version 4

Fixes issues like lquixada/cross-fetch#78, enabling to run web3.js in service worker

* update web3-providers-http CHANGELOG.md

---------

Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants