Skip to main content

Web and Service Workers

Beta

This page documents the Bugsee JavaScript SDK, which is in beta (0.1.x) and can change between betas.

@bugsee/webworker is the SDK for Web Workers (dedicated and shared) and Service Workers. It captures console output, network traffic, and global error and unhandled-rejection events in a worker context that has no DOM.

A worker is a separate JavaScript context, so it needs its own launch. Your page code keeps using @bugsee/bugsee; call @bugsee/webworker from inside the worker script.

Requirements​

  • A browser with worker support. Service Workers also need a secure context.
  • An app token. Create a JavaScript application in Bugsee and choose Web Worker or Service Worker as its target; see Create your app.

Install​

npm install @bugsee/webworker

Web Worker​

launch() returns the client directly, not a Promise.

worker.ts
import { launch } from '@bugsee/webworker';

const bugsee = launch('<your_app_token>');

The SDK captures console output, fetch and WebSocket traffic (also XMLHttpRequest in dedicated workers), and global error and unhandledrejection events on the worker.

A worker has no DOM, so these are not captured: clicks, key presses, navigation, the view hierarchy and session replay.

Service Worker​

A Service Worker is stopped when idle, so wrap event handlers with withBugseeEvent. It hands the upload to event.waitUntil, so the worker stays alive until a report is sent.

sw.ts
import { launch, withBugseeEvent } from '@bugsee/webworker';

const bugsee = launch('<your_app_token>', { platformType: 'service-worker' });

self.addEventListener(
'fetch',
withBugseeEvent(bugsee, (event) => {
event.respondWith(fetch(event.request));
}),
);

The SDK detects a Service Worker from its runtime, so platformType is only needed to override the detection.

withBugseeEvent never changes your handler's outcome: your own error is rethrown, and SDK failures go to an optional third argument:

withBugseeEvent(bugsee, handler, (error) => console.debug(error));

What is exported​

ExportDescription
launch(appToken, options?)Starts the SDK and returns the client synchronously.
withBugseeEvent(client, handler, onError?)Wraps an event handler so the upload runs inside event.waitUntil.
relaunchRestarts the SDK.
getStatus, BugseeStatus, LifecycleEvents, addReportAttachment, getDefaultNetworkSanitizerThe shared client exports.

Options​

OptionTypeDefaultDescription
platformType'web-worker' | 'service-worker'detectedWhich kind of worker this is. It also decides the persist default.
persistbooleantrue for Service Workers, false for Web WorkersKeep assembled report bundles in IndexedDB and re-upload any that a stopped worker left behind.
recoverbooleantrueRe-upload bundles that a previous activation left behind, on the next launch.
appVersion, appBuild, appIdstringresolvedIdentify your release in reports.
captureLogs, captureNetworkbooleantrueCapture streams.

See Configuration for the shared options.

App version and build​

Pass appVersion and appBuild to launch. The Vite and webpack plugins can also inject __BUGSEE_APP_VERSION__ and __BUGSEE_APP_BUILD__ at build time, or you can set them as globals on self before launching. If none is found, the SDK logs one warning and reports version 0.0.0 and build 0.

Source maps​

Workers are separate bundles, so make sure your bundler plugin covers the worker entry. See Symbolication.

Verify​

  1. Launch the SDK in the worker script.
  2. Report a test error from the worker:
bugsee.logException(new Error('Bugsee test error'));
  1. Open the Bugsee dashboard. The report appears with the worker's console and network data.
Found an issue, typo, or wrong statement on this page? Report it now →