Use fetch if XMLHttpRequest not available
See original GitHub issueDo 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:
- Created 6 years ago
- Reactions:2
- Comments:7 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
Fixed in https://github.com/getsentry/raven-js/pull/1157