-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
adding isomorphic support #31
Conversation
I think this polyfill is only going to be targeting browsers, not server side environments. |
@josh Would you still be against this if the node bits were put in their own file keeping the main polyfill clean of node code? ie.
require('es6-promise').polyfill();
global.XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
require('./fetch'); Alternatively, I could put this stuff in a new repo that pulls your fetch in as a dependency (that new repo could also be published on npm, which would solve the problem for people who want isomorphic |
return | ||
} | ||
|
||
var Promise = global.Promise; | ||
if (typeof Promise === 'undefined') { | ||
Promise = require('bluebird'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will mean that bluebird's code will always be added to this file when it's browserified and even delivered to browsers that already support Promises… This will make the compiled code a lot bigger — not sure you want to do it like this… I've written an alternative suggestion in the comments.
I think a node implementation ought to be a separate project. It'd probably
|
|
||
var XMLHttpRequest = global.XMLHttpRequest; | ||
if (typeof XMLHttpRequest === 'undefined') { | ||
XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again I suspect this might bloat the client side code when compiled with browserify – you might be shipping an XMLHttpRequest
polyfill to browsers… which I don't think would ever be used
You don't like my 3 line solution? It's very maintainable :D I do agree though that it would be best ( from a performance perspective ) if it were talking direct to node apis rather than through a polyfill designed for browser but I think if you're using this library performance is probably a less of a concern than being able to write reusable code (if you were worrying about performance you shouldn't use a promise based ajax library — you should use one that implements streams). Given this is so small & quick I'll make this into a separate repo for now? |
or smallest change with removing 'window' and inject with global this will help noder to use it in isomorphic way I think. |
@Jxck I'm having another problem – Compare this:- echo 'console.log(this === global);' > include-me.js && \
node -e "require('./include-me'); ";
// => false With this:- node -e "console.log(global === this);"
// => true Am I doing something wrong? |
If I'm understanding this properly, the request is for this web browser API polyfill be changed to work in a Node.js server runtime. The Fetch specification is written with browser constraints in mind. As the spec evolves, so will the polyfill. It's going to, as strictly as possible for a polyfill, match the specification without concern for other possible runtimes. Node.js already contains API for making HTTP requests. If there is a need to wrap that in a fetch-like API, it can be implemented in a separate project that can take full advantage of Node.js server features. Let's allow this implementation to optimize for browsers and another for Node.js. |
Sorry @dgraham & @josh, I really liked @Jxck's idea of being able to use the same code using Fetch API on the client and the server (if only for prototypes) so I made it into a separate thing here… https://github.com/matthew-andrews/isomorphic-fetch [Please don't suspend my GitHub account 😶] |
⚡ |
😇 |
nice! |
@matthew-andrews in node, module.exports === exports === global this // fetch.js
this.fetch = function() {}; <script src='fetch.js'>
<script>
window.fetch()
</script>
and |
I really know this spec is for browser.
but I believe make it isomorphic works fine for developer.
if I use this API on both browser and server.
it will help boost javascript isomorphic.
ex)