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

Handle cases where self isn't defined #253

Merged
merged 1 commit into from
Jan 19, 2016

Conversation

billinghamj
Copy link
Contributor

As per: #125 (comment)

Resolves #252, #238, #165, #125, #124, and probably a few others.

There are a lot of different ways this code could be written. If you have style preferences contrary to the way I've done this, I'd be very happy to change it.

@mislav
Copy link
Contributor

mislav commented Jan 9, 2016

Looks good!

@dgraham Any objections?

@dgraham
Copy link
Contributor

dgraham commented Jan 9, 2016

Invoking the wrapper function with this resolves to window, which is how we started all the way back in c1af6de. This looks good to me. :shipit:

@chrisirhc
Copy link

Wouldn't this still throw a ReferenceError exception where self isn't defined? How about doing a typeof check like typeof self !== 'undefined' ? self : this ?

@mislav
Copy link
Contributor

mislav commented Jan 10, 2016

Good point @chrisirhc. Someone should test this out in an isomorphic environment and report back. I have no idea how it would handle the self || this construct.

@billinghamj
Copy link
Contributor Author

Hmm, yes that is true. Another option could be:

this.self || this

Which works exactly the same way but should not throw.

@billinghamj
Copy link
Contributor Author

I have updated the code as per the above comment.

@mislav
Copy link
Contributor

mislav commented Jan 11, 2016

Can someone test this out in an isomorphic environment, as per people's complaints on aforementioned threads?

@billinghamj
Copy link
Contributor Author

Node - master branch:

$ node
> var a = require('./fetch')
ReferenceError: self is not defined
    at /Users/james/Projects/fetch/fetch.js:4:7
    at Object.<anonymous> (/Users/james/Projects/fetch/fetch.js:381:3)
    at Module._compile (module.js:398:26)
    at Object.Module._extensions..js (module.js:405:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at repl:1:9
    at REPLServer.defaultEval (repl.js:254:27)

Node - this PR:

$ node
> var a = require('./fetch')
undefined
> a
{ Headers: [Function: Headers],
  Request: [Function: Request],
  Response: { [Function: Response] error: [Function], redirect: [Function] },
  fetch: { [Function] polyfill: true } }

React Native - master
React Native - this PR (this new error is a separate issue from isomorphic-fetch)

I can't speak for other environments though

billinghamj added a commit to billinghamj/isomorphic-fetch that referenced this pull request Jan 11, 2016
billinghamj added a commit to billinghamj/isomorphic-fetch that referenced this pull request Jan 11, 2016
@@ -378,4 +378,4 @@
})
}
self.fetch.polyfill = true
})();
})(this.self || this);
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like I would like to revert to @chrisirhc's idea by doing

typeof self !== 'undefined' ? self : this

simply because that approach has been better battle-tested by compilers such as Browserify.

@billinghamj
Copy link
Contributor Author

@mislav updated as-per your comment

mislav added a commit that referenced this pull request Jan 19, 2016
Handle cases where `self` isn't defined
@mislav mislav merged commit f4fc29f into JakeChampion:master Jan 19, 2016
@mislav
Copy link
Contributor

mislav commented Jan 19, 2016

Thank you!

@yorkie
Copy link

yorkie commented Jan 19, 2016

Hey @mislav did you forget to publish to npm, when I install 0.11.0 get the following:

pm ERR! Darwin 15.2.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "i"
npm ERR! node v5.0.0
npm ERR! npm  v3.3.6

npm ERR! No compatible version found: [email protected]
npm ERR! Valid install targets:
npm ERR! ["0.10.1","0.10.0","0.9.0","0.8.2","0.8.1","0.8.0","0.7.0","0.6.1","0.6.0","0.5.0"]
npm ERR!
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/yorkieliu/workspace/weflex/gian/npm-debug.log

@mislav
Copy link
Contributor

mislav commented Jan 19, 2016

@yorkie Something's wrong with our build system and auto-publish on npm. Stay tuned.

@yorkie
Copy link

yorkie commented Jan 19, 2016

I see, is there something I can help? This is no hurry, but if we can get a working notification with npm, that would be so sweet :-)

@mislav
Copy link
Contributor

mislav commented Jan 19, 2016 via email

@yorkie
Copy link

yorkie commented Jan 19, 2016

It's working now, very thanks to you @mislav 👍

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Possibility for a 'self' polyfill
5 participants