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
:
npm install --save @sentry/browser
Then you can use it:
index.js
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!");
Manual Capturing
To capture errors or messages manually, such as to use captureMessage
or captureException
inside Web Workers, Sentry should be initialized inside each Web Workers' own scope. Only unhandled errors will be captured and sent to Sentry without worker-level initialization.
worker.js
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
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.
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").