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

require('isomorphic-fetch/safe') to not set globals #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

gobengo
Copy link

@gobengo gobengo commented May 8, 2015

This pull allows users of the library to do

var fetch = require('isomorphic-fetch/safe');
require('assert')(typeof global.fetch === 'undefined');

'safe' requires do not result in any new global.fetch.

I'd like to use this library as a dependency in a lib I'm working on which, when run in the browser, should not touch global scope. This PR makes that use case possible without having to fork the lib.

Behavior of require('isomorphic-fetch') should remain unchanged (ie it will return the same thing and continue setting global.fetch on server and browser).

@matthew-andrews
Copy link
Owner

This would only work on the server, not the client…… and making things consistent between server + client is perhaps the main purpose of this repository 😄.

I don't understand why people want this…… isomorphic-fetch does almost nothing except for define globals.

If you want to do something in a more controlled way then I think you'd be better off using the underlying libraries that iso-fetch pulls in……

$ npm install --save node-fetch whatwg-fetch

Then in client side do:-

var fetch = require('whatwg-fetch');

And on the server do:-

var fetch = require('node-fetch');

@gobengo
Copy link
Author

gobengo commented Jul 11, 2015

It does work in the client... I used this fork in https://github.com/Livefyre/livefyre-geo-collection

"I don't understand why people want this……". I mentioned in the PR description: "I'd like to use this library as a dependency in a lib I'm working on which, when run in the browser, should not touch global scope. This PR makes that use case possible without having to fork the lib."

No sweat on closing it though. If anyone wants a version of this that doesn't create globals, use my fork. File an issue if you want me to rebase.

@matthew-andrews
Copy link
Owner

Hmm, thank you for replying with the extra details. I think I see what you mean now. That's tricky.

Normally for libraries like yours' I would try to encourage the consumer to bring their own polyfills. It's because of the reason why this library (or denodeify) don't ship with a Promise polyfill, which they depend on to actually work in all but the latest browsers.

But on the other hand I can see why for a component like yours' you would want to make developers' lives as easy as possible [whilst at the same time not polluting global scope].

I'm going to think about this for a few days. Thank you again.

if (/^\/\//.test(url)) {
url = 'https:' + url;
}
return require('node-fetch').call(this, url, options);

Choose a reason for hiding this comment

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

I would recommend caching this require call outside of the function and ensure it closes over it. Without this change, I believe you would be hitting the synchronous require with each fetch.

@billinghamj
Copy link

Presumably the main blocker to this is the fact that GitHub's implementation cannot be used without polluting the global scope. Due to the fact that they refuse to implement module support, this can't be changed.

@JakeChampion
Copy link

@gobengo Do you have a reference to the fact about Github refusing to implement module support? I was about to start work on creating a fetch polyfill which didn't shim by default (pollute globals) and was going to submit it as a patch to Github's fetch polyfill.

@billinghamj
Copy link

@paulmelnikow
Copy link
Contributor

@JakeChampion I'm curious, did you get around to doing that? If not, you might be interested in this.

I recently worked with @qubyte to implement isomorphism and bundler-agnosticism in fetch-ponyfill. It's a grand hack which processes Github's implementation in a build step, wrapping it in a closure and extracting it via thisArg. Works great in Node, Webpack, and Browserify.

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.

5 participants