Skip to content

Commit a1a721b

Browse files
committed
ref(replay): Improve isBrowser check
1 parent 0522caa commit a1a721b

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

packages/replay/jest.setup.ts

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ global.__SENTRY_REPLAY_VERSION__ = 'version:Test';
1010

1111
type MockTransport = jest.MockedFunction<Transport['send']>;
1212

13+
jest.mock('./src/util/isBrowser', () => {
14+
return {
15+
isBrowser: () => true,
16+
};
17+
});
18+
1319
afterEach(() => {
1420
const hub = getCurrentHub();
1521
if (typeof hub?.getClient !== 'function') {

packages/replay/src/index.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { captureInternalException } from './util/captureInternalException';
3838
import { createBreadcrumb } from './util/createBreadcrumb';
3939
import { createPayload } from './util/createPayload';
4040
import { dedupePerformanceEntries } from './util/dedupePerformanceEntries';
41+
import { isBrowser } from './util/isBrowser';
4142
import { isExpired } from './util/isExpired';
4243
import { isSessionExpired } from './util/isSessionExpired';
4344

@@ -53,8 +54,6 @@ const MEDIA_SELECTORS = 'img,image,svg,path,rect,area,video,object,picture,embed
5354

5455
let _initialized = false;
5556

56-
const isBrowser = typeof window !== 'undefined';
57-
5857
export class Replay implements Integration {
5958
/**
6059
* @inheritDoc
@@ -210,7 +209,7 @@ export class Replay implements Integration {
210209
maxWait: this.options.flushMaxDelay,
211210
});
212211

213-
if (isBrowser && _initialized) {
212+
if (isBrowser() && _initialized) {
214213
const error = new Error('Multiple Sentry Session Replay instances are not supported');
215214
captureInternalException(error);
216215
throw error;
@@ -229,7 +228,7 @@ export class Replay implements Integration {
229228
* here to avoid any future issues.
230229
*/
231230
setupOnce(): void {
232-
if (!isBrowser) {
231+
if (!isBrowser()) {
233232
return;
234233
}
235234
// XXX: See method comments above
@@ -243,7 +242,7 @@ export class Replay implements Integration {
243242
* PerformanceObserver, Recording, Sentry SDK, etc)
244243
*/
245244
start(): void {
246-
if (!isBrowser) {
245+
if (!isBrowser()) {
247246
return;
248247
}
249248

@@ -309,7 +308,7 @@ export class Replay implements Integration {
309308
* does not support a teardown
310309
*/
311310
stop(): void {
312-
if (!isBrowser) {
311+
if (!isBrowser()) {
313312
return;
314313
}
315314

packages/replay/src/util/isBrowser.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { isNodeEnv } from '@sentry/utils';
2+
3+
export function isBrowser(): boolean {
4+
return typeof window !== 'undefined' && !isNodeEnv();
5+
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const isBrowser = typeof window !== 'undefined';
1+
import { isBrowser } from './isBrowser';
22

33
/**
44
* Returns true if we are currently recording an internal to Sentry replay
55
* (e.g. on https://sentry.io )
66
*/
77
export function isInternal(): boolean {
8-
return isBrowser && ['sentry.io', 'dev.getsentry.net'].includes(window.location.host);
8+
return isBrowser() && ['sentry.io', 'dev.getsentry.net'].includes(window.location.host);
99
}

0 commit comments

Comments
 (0)