lquixada / cross-fetch

Universal WHATWG Fetch API for Node, Browsers and React Native.
MIT License
1.67k stars 104 forks source link

react-native doesn't require a ponyfill as it already polyfills fetch #39

Closed msluther closed 5 years ago

msluther commented 5 years ago

This is an attempt to fix this issue such that:

  1. The bundle size can be smaller as it won't need to include another implementation of fetch
  2. Timing/global state issues are addressed and global.fetch isn't set to false
msluther commented 5 years ago

Seems like this old PR was also seeing the global fetch getting stomped as well. This feels like a better fix though and more understandable.

lquixada commented 5 years ago

Your work is great but there's something missing: a dummy react-native polyfill. The reason why this is important is that, given the same js file, it is supposed to work regardless of the environment. Thus, this snippet:

import 'cross-fetch/polyfill'

fetch('https://api.github.com/users/lquixada')
  .then(res => {
    if (res.status >= 400) {
      throw new Error('Bad response from server')
    }
    return res.json()
  })
  .then(user => {
    console.log(user)
  })
  .catch(err => {
    console.error(err)
  })

would work in the node and in the browser environments but it would break in react-native. So my suggestion is to create a react-native polyfill that does nothing. :)

msluther commented 5 years ago

Good call on the polyfill. Will do that as well.

lquixada commented 5 years ago

Thanks! Awesome work!