question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use fetch if XMLHttpRequest not available

See original GitHub issue

Do you want to request a feature or report a bug? Feature

What is the current behavior? When attempting to use raven-js in a ServiceWorker, Raven._makeRequest will fail because XMLHttpRequest is not available to SW environments.

What is the expected behavior? Use the fetch api when available.

Which versions of Raven.js, and which browser and OS are affected by this issue? All

Did this work in previous versions of Raven.js? No

Are you using the CDN (http://ravenjs.com)? No

Are you using hosted Sentry or on-premises? Hosted

I put in a really quick hack into our /sw.js to make it work:

Raven._makeRequest = (opts) => {
  const auth = Object.keys(opts.auth).map((key) => `${key}=${opts.auth[key]}`).join('&');
  const headers = new Headers();
  headers.append('origin', self.location.origin);
  const req = new Request(`${opts.url}?${auth}`, {
    method: 'POST',
    body: new Blob([ JSON.stringify({ ...opts.data, ...opts.auth }, null, 2)], { type: 'application/json' }),
    mode: 'cors',
    credentials: 'omit',
    headers,
    referrer: self.location.origin
  });

  fetch(req);
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
LewisJElliscommented, Apr 26, 2017

Interesting, I was surprised to hear that there’s an environment with fetch but not XHR, but after reading up on SW docs and this chromium issue on the XHR removal it makes sense.

I’ll add this to the todos.

0reactions
kamilogorekcommented, Nov 30, 2017
Read more comments on GitHub >

github_iconTop Results From Across the Web

XMLHttpRequest and Fetch not functioning - Stack Overflow
I am using OpenWeatherMap I have an API key and it works as a plain url but when it the script it doesn't....
Read more >
The Fetch API. A modern replacement for XMLHttpRequest
Fetch is a new native JavaScript API, supported by most browsers today. Fetch allows you to make network requests similar to XMLHttpRequest ....
Read more >
Fetch API - Replacement for XMLHttpRequest (XHR) - Atatus
The Fetch API allows you to make network requests similar to XMLHttpRequest (XHR). The main difference is that the Fetch API uses Promises, ......
Read more >
How to Use the Fetch API (Correctly) - CODE Magazine
Navigate into the folder Samples-WebAPI and load that folder in Visual Studio Code or Visual Studio 2019. Open the appsettings.json file and ...
Read more >
XMLHttpRequest vs the Fetch API for Ajax - SitePoint
Unlike XMLHttpRequest , not all implementations of Fetch will send cookies so your application's authentication could fail. The problem can be ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found