Web Workers

Learn how to use Sentry's Browser SDK in Web Workers API.

Sentry's Browser SDK supports Web Workers API. To capture unhandled errors from Web Workers:

Install @sentry/browser using yarn or npm:

Copied
npm install --save @sentry/browser

Then you can use it:

index.js
Copied
import * as Sentry from "@sentry/browser";

Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
});

const worker = new Worker("worker.js");

// Errors from `onmessage` callback of `worker.js`
// will be captured.
worker.postMessage("Hello!");

worker.js
Copied
import * as Sentry from "@sentry/browser";

self.onmessage = (message) => {
  // This will fail silently.
  Sentry.captureMessage("Message received");

  // This error will be captured.
  throw new Error();
};

worker.js
Copied
import * as Sentry from "@sentry/browser";

Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
});

self.onmessage = (message) => {
  // This message will be captured
  Sentry.captureMessage("Message received");

  // This error will also be captured.
  throw new Error();
};

Note, that if you use non-default integrations inside web workers, they may not function as expected. But non-default integrations that are enabled outside of a worker’s scope won’t be affected and will function as expected.

Sentry's source maps integration is supported inside Web Workers, if provided. Learn more about providing your source maps to Sentry.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").