Skip to content

Use fetch if XMLHttpRequest not available #930

Closed
@paularmstrong

Description

@paularmstrong

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);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions