Giter Site home page Giter Site logo

self is not defined about fetch HOT 10 CLOSED

github avatar github commented on July 27, 2024 1
self is not defined

from fetch.

Comments (10)

billinghamj avatar billinghamj commented on July 27, 2024 1

Unfortunately in React Native, the workarounds mentioned don't seem to be sufficient. Due to the way React packages JS files assuming they support AMD/CommonJS modularization properly, this library ends up running anyway - even if it isn't required.

from fetch.

tcurdt avatar tcurdt commented on July 27, 2024

just found https://github.com/matthew-andrews/isomorphic-fetch and then https://github.com/matthew-andrews/fetch/blob/master/fetch.js#L3 which has:

if (typeof global !== 'undefined') {
  var self = global.self;
}

from fetch.

mislav avatar mislav commented on July 27, 2024

Yes please use that project instead. #31

from fetch.

tcurdt avatar tcurdt commented on July 27, 2024

I don't want to really use it in isomorphic way - I just have an isomorphic setup. That's a bit of a different thing. (Which is why those projects are not really what I am after)

Ideally require("whatwg-fetch") would just export what it's offering.
What I ended up doing is:

if (process.env.IS_BROWSER) {
  require('whatwg-fetch');
}

Not nice - but works.

from fetch.

mislav avatar mislav commented on July 27, 2024

Your workaround looks good. I would suggest applying the same conditional to all requires in your app that load libraries that are only useful in browsers.

from fetch.

tcurdt avatar tcurdt commented on July 27, 2024

I will.

As a side note - I would much prefer this style of use

var blafetch = require('whatwg-fetch');
blafetch('/users.html')
  .then(function(response) {
    return response.text()
  }).then(function(body) {
    document.body.innerHTML = body
  })

or even better

var Fetch = require('whatwg-fetch');
Fetch.get('/users.html')
  .then(function(response) {
    return response.text()
  }).then(function(body) {
    document.body.innerHTML = body
  })

Maybe for the next major version.

from fetch.

mislav avatar mislav commented on July 27, 2024

As a side note - I would much prefer this style of use

Perhaps you're missing the point of this project. We're trying to create a polyfill for a future standard feature of browsers. The API for that feature is window.fetch. We're not trying to create anything more or other than that.

from fetch.

tcurdt avatar tcurdt commented on July 27, 2024

I see.

from fetch.

mislav avatar mislav commented on July 27, 2024

@billinghamj We welcome a PR that solves this while keeping in mind that the code must also be able to run in a web worker environment. See #239 (comment) for context

from fetch.

billinghamj avatar billinghamj commented on July 27, 2024

@mislav That was the purpose of #252. I'll make a PR for it.

from fetch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.