Skip to content

Commit 7698f61

Browse files
authored
fix(browser): Use DedicatedWorkerGlobalScope global object type in registerWebWorker (#17447)
Changes the type from `WebWorker` to `DedicatedWorkerGlobalScope`*. Other worker types don't support `postMessage`, so this type correctly scopes the API to its intended use (i.e. in dedicated workers).
1 parent 090e7cc commit 7698f61

File tree

6 files changed

+23
-15
lines changed

6 files changed

+23
-15
lines changed

dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as Sentry from '@sentry/browser';
22

3-
// type cast necessary because TS thinks this file is part of the main
4-
// thread where self is of type `Window` instead of `Worker`
5-
Sentry.registerWebWorker({ self: self as unknown as Worker });
3+
Sentry.registerWebWorker({ self });
64

75
// Let the main thread know the worker is ready
86
self.postMessage({

dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker2.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as Sentry from '@sentry/browser';
22

3-
// type cast necessary because TS thinks this file is part of the main
4-
// thread where self is of type `Window` instead of `Worker`
5-
Sentry.registerWebWorker({ self: self as unknown as Worker });
3+
Sentry.registerWebWorker({ self });
64

75
// Let the main thread know the worker is ready
86
self.postMessage({

dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker3.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as Sentry from '@sentry/browser';
22

3-
// type cast necessary because TS thinks this file is part of the main
4-
// thread where self is of type `Window` instead of `Worker`
5-
Sentry.registerWebWorker({ self: self as unknown as Worker });
3+
Sentry.registerWebWorker({ self });
64

75
// Let the main thread know the worker is ready
86
self.postMessage({

dev-packages/e2e-tests/test-applications/browser-webworker-vite/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"target": "ES2022",
44
"useDefineForClassFields": true,
55
"module": "ESNext",
6-
"lib": ["ES2022", "DOM", "DOM.Iterable"],
7-
"skipLibCheck": true,
6+
"lib": ["ES2022", "DOM", "DOM.Iterable", "WebWorker"],
7+
"skipLibCheck": false,
8+
"skipDefaultLibCheck": true,
89

910
/* Bundler mode */
1011
"moduleResolution": "bundler",

packages/browser/src/integrations/webWorker.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface WebWorkerIntegration extends Integration {
2525
* any messages from the worker. Otherwise, your message handlers will receive
2626
* messages from the Sentry SDK which you need to ignore.
2727
*
28-
* This integration only has an effect, if you call `Sentry.registerWorker(self)`
28+
* This integration only has an effect, if you call `Sentry.registerWebWorker(self)`
2929
* from within the worker(s) you're adding to the integration.
3030
*
3131
* Given that you want to initialize the SDK as early as possible, you most likely
@@ -113,8 +113,21 @@ function listenForSentryDebugIdMessages(worker: Worker): void {
113113
});
114114
}
115115

116+
/**
117+
* Minimal interface for DedicatedWorkerGlobalScope, only requiring the postMessage method.
118+
* (which is the only thing we need from the worker's global object)
119+
*
120+
* @see https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope
121+
*
122+
* We can't use the actual type because it breaks everyone who doesn't have {"lib": ["WebWorker"]}
123+
* but uses {"skipLibCheck": true} in their tsconfig.json.
124+
*/
125+
interface MinimalDedicatedWorkerGlobalScope {
126+
postMessage: (message: unknown) => void;
127+
}
128+
116129
interface RegisterWebWorkerOptions {
117-
self: Worker & { _sentryDebugIds?: Record<string, string> };
130+
self: MinimalDedicatedWorkerGlobalScope & { _sentryDebugIds?: Record<string, string> };
118131
}
119132

120133
/**
@@ -125,7 +138,7 @@ interface RegisterWebWorkerOptions {
125138
* import * as Sentry from '@sentry/<your-sdk>';
126139
*
127140
* // Do this as early as possible in your worker.
128-
* Sentry.registerWorker({ self });
141+
* Sentry.registerWebWorker({ self });
129142
*
130143
* // continue setting up your worker
131144
* self.postMessage(...)

packages/browser/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"include": ["src/**/*", "test/loader.js"],
55

66
"compilerOptions": {
7-
"lib": ["DOM", "ES2018"],
7+
"lib": ["DOM", "ES2018", "WebWorker"]
88
}
99
}

0 commit comments

Comments
 (0)