Skip to content

Commit fdee17e

Browse files
authored
fix(tracing): Prevent metrics erroring module load in web workers (#3941)
Minimal fix for #3934
1 parent 949f1b6 commit fdee17e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

packages/tracing/src/browser/metrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class MetricsInstrumentation {
2323
private _clsEntry: LayoutShift | undefined;
2424

2525
public constructor() {
26-
if (!isNodeEnv() && global?.performance) {
26+
if (!isNodeEnv() && global?.performance && global?.document) {
2727
if (global.performance.mark) {
2828
global.performance.mark('sentry-tracing-init');
2929
}

packages/tracing/test/browser/metrics.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,25 @@ describe('MetricsInstrumentation', () => {
183183
trackers.forEach(tracker => expect(tracker).not.toBeCalled());
184184
});
185185

186-
it('initializes trackers when not on node and `global.performance` is available.', () => {
186+
it('does not initialize trackers when not on node but `global.document` is not available (in worker)', () => {
187+
// window not necessary for this test, but it is here to exercise that it is absence of document that is checked
188+
addDOMPropertiesToGlobal(['performance', 'addEventListener', 'window']);
189+
const processBackup = global.process;
190+
global.process = undefined;
191+
const documentBackup = global.document;
192+
global.document = undefined;
193+
194+
const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
195+
jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
196+
);
197+
new MetricsInstrumentation();
198+
global.process = processBackup;
199+
global.document = documentBackup;
200+
201+
trackers.forEach(tracker => expect(tracker).not.toBeCalled());
202+
});
203+
204+
it('initializes trackers when not on node and `global.performance` and `global.document` are available.', () => {
187205
addDOMPropertiesToGlobal(['performance', 'document', 'addEventListener', 'window']);
188206
const backup = global.process;
189207
global.process = undefined;

0 commit comments

Comments
 (0)