Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

XMLHttpRequest is not defined - Does not work on Cloudflare Worker #72

Closed
MickL opened this issue Jan 1, 2020 · 4 comments
Closed

XMLHttpRequest is not defined - Does not work on Cloudflare Worker #72

MickL opened this issue Jan 1, 2020 · 4 comments

Comments

@MickL
Copy link

MickL commented Jan 1, 2020

I want to use timber inside of a serverless environment "Cloudflare Worker". There XMLHttpRequest is not defined, instead there is fetch().

I tried both @timberio/node and @timberio/node but both import cross-fetch browser-ponyfill.js which throws this error.

Is there a way to use timberio without cross-fetch? Or at least without any polyfill?
Why is browser-ponyfill imported even tho i used @timberio/node?

@MickL
Copy link
Author

MickL commented Jan 1, 2020

I tested removing import fetch from "cross-fetch"; in @timberio/browser/dist/es6/browser.js and @timberio/node/dist/es6/node.js and then it works for both browser and node module.

@leebenson
Copy link
Member

As I commented just now in another issue, those two packages are designed specifically for Node.js and browser environments, where fetch() either doesn't exist or sometimes exists and needs polyfilling.

For the likes of Cloudflare Workers which operate in their own custom sandbox, we could probably do with a separate package that's specifically tailed for that environment.

In the meantime, the workaround I'd recommend is copying this code to your project from @timberio/browser and dropping the fetch import. You can then import your project's own class and reference that instead of the one provided out-the-box by the browser package.

If you find you need to customize further, another approach is to extend @timberio/core's Base class directly with your own implementation. An example is provided in the docs here. That way, you can control specifically how you want data to make its way to Timber, whether or not to use throttling/buffering and any other parameters that are necessary or optimal for your environment.

@MickL
Copy link
Author

MickL commented Jan 2, 2020

Thanks this sound like a solid workarounds for now.

@MickL
Copy link
Author

MickL commented Jan 3, 2020

An even more easy fix when using Webpack:

// webpack.config.js

module.exports = {
  //...
  externals: {
    fetch: 'cross-fetch'
  }
};

Update: This is not working somehow. If someone knows why, please comment :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants