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 illegal invocation errors #47

Merged
merged 3 commits into from
Jun 28, 2017

Conversation

jhnns
Copy link
Contributor

@jhnns jhnns commented Jun 26, 2017

Fixes #46

I can also confirm the fix in my local setup.

src/index.js Outdated
@@ -1,4 +1,4 @@
export default typeof fetch=='function' ? fetch : function(url, options) {
export default typeof fetch == 'function' && typeof window != 'undefined' ? fetch.bind(window) : function(url, options) {
Copy link
Owner

Choose a reason for hiding this comment

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

Seems to work if you just do typeof fetch=='function' ? fetch.bind() : - thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha nice one! 👍

@developit developit merged commit 04c6d24 into developit:master Jun 28, 2017
@jhnns jhnns deleted the fix/illegal-invocation branch June 28, 2017 17:16
@balloob
Copy link
Contributor

balloob commented Jul 7, 2017

This doesn't fix it for me. Changing it to fetch.bind(window) does work.

@developit would you be open for a PR that changes it to explicitly bind it to window?

@balloob
Copy link
Contributor

balloob commented Jul 7, 2017

Never mind. It is possible to fix it in the Webpack config without updating unfetch. Add this to your Webpack config (source):

{
  …,
  module: {
    strictThisContextOnImports: true,
  },
  …
}

@developit
Copy link
Owner

How are you testing that it didn't work @balloob?

@balloob
Copy link
Contributor

balloob commented Jul 7, 2017

Sorry, it's in a private repo at work so don't have a minimal work around. Was using Webpack 3.1 and Unfetch 3.0. When I changed bind() to bind(window) I would not get the invocation error.

Can take a stab on Monday to get a minimal repro.

@developit
Copy link
Owner

That would be good - I was testing in Safari and .bind() was working. Perhaps it's not in another browser?

@balloob
Copy link
Contributor

balloob commented Jul 9, 2017

I was testing it in Chrome.

@aga5tya
Copy link

aga5tya commented Jul 11, 2017

@developit @balloob I'm using isomorphic-unfetch, confirming that i still get this error on chrome.

Edit: works just fine on safari.

@developit
Copy link
Owner

Hmm - can you try this in your console and tell me if it fails?

let unfetch = fetch.bind();
unfetch('/').then(console.log)

Works for me in Chrome 59+

@aga5tya
Copy link

aga5tya commented Jul 11, 2017

@developit No errors when i run the snippet in chrome console. As @balloob was mentioning, issue happens within the app only when i bundle with webpack 3.1 and run the app on chrome. Fyi I'm on chrome 60 beta version.

@developit
Copy link
Owner

One thing I just thought of - Function.prototype.bind is IE9+

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.

TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
6 participants