From 0bd15272853cd89189e624978b5e309ede94d83a Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Tue, 29 Nov 2022 12:42:45 +0100 Subject: [PATCH] ref(replay): Improve `isBrowser` check --- packages/replay/jest.setup.ts | 6 ++++++ packages/replay/src/index.ts | 11 +++++------ packages/replay/src/util/isBrowser.ts | 5 +++++ packages/replay/src/util/isInternal.ts | 4 ++-- 4 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 packages/replay/src/util/isBrowser.ts diff --git a/packages/replay/jest.setup.ts b/packages/replay/jest.setup.ts index f5b3a6fa5408..f4f6e33ed840 100644 --- a/packages/replay/jest.setup.ts +++ b/packages/replay/jest.setup.ts @@ -10,6 +10,12 @@ global.__SENTRY_REPLAY_VERSION__ = 'version:Test'; type MockTransport = jest.MockedFunction; +jest.mock('./src/util/isBrowser', () => { + return { + isBrowser: () => true, + }; +}); + afterEach(() => { const hub = getCurrentHub(); if (typeof hub?.getClient !== 'function') { diff --git a/packages/replay/src/index.ts b/packages/replay/src/index.ts index 49dd2730d9ef..7e72b7eb7294 100644 --- a/packages/replay/src/index.ts +++ b/packages/replay/src/index.ts @@ -38,6 +38,7 @@ import { captureInternalException } from './util/captureInternalException'; import { createBreadcrumb } from './util/createBreadcrumb'; import { createPayload } from './util/createPayload'; import { dedupePerformanceEntries } from './util/dedupePerformanceEntries'; +import { isBrowser } from './util/isBrowser'; import { isExpired } from './util/isExpired'; import { isSessionExpired } from './util/isSessionExpired'; @@ -53,8 +54,6 @@ const MEDIA_SELECTORS = 'img,image,svg,path,rect,area,video,object,picture,embed let _initialized = false; -const isBrowser = typeof window !== 'undefined'; - export class Replay implements Integration { /** * @inheritDoc @@ -210,7 +209,7 @@ export class Replay implements Integration { maxWait: this.options.flushMaxDelay, }); - if (isBrowser && _initialized) { + if (isBrowser() && _initialized) { const error = new Error('Multiple Sentry Session Replay instances are not supported'); captureInternalException(error); throw error; @@ -229,7 +228,7 @@ export class Replay implements Integration { * here to avoid any future issues. */ setupOnce(): void { - if (!isBrowser) { + if (!isBrowser()) { return; } // XXX: See method comments above @@ -243,7 +242,7 @@ export class Replay implements Integration { * PerformanceObserver, Recording, Sentry SDK, etc) */ start(): void { - if (!isBrowser) { + if (!isBrowser()) { return; } @@ -309,7 +308,7 @@ export class Replay implements Integration { * does not support a teardown */ stop(): void { - if (!isBrowser) { + if (!isBrowser()) { return; } diff --git a/packages/replay/src/util/isBrowser.ts b/packages/replay/src/util/isBrowser.ts new file mode 100644 index 000000000000..5093781bc4dd --- /dev/null +++ b/packages/replay/src/util/isBrowser.ts @@ -0,0 +1,5 @@ +import { isNodeEnv } from '@sentry/utils'; + +export function isBrowser(): boolean { + return typeof window !== 'undefined' && !isNodeEnv(); +} diff --git a/packages/replay/src/util/isInternal.ts b/packages/replay/src/util/isInternal.ts index 8249fa8f508a..e6e920e4f058 100644 --- a/packages/replay/src/util/isInternal.ts +++ b/packages/replay/src/util/isInternal.ts @@ -1,9 +1,9 @@ -const isBrowser = typeof window !== 'undefined'; +import { isBrowser } from './isBrowser'; /** * Returns true if we are currently recording an internal to Sentry replay * (e.g. on https://sentry.io ) */ export function isInternal(): boolean { - return isBrowser && ['sentry.io', 'dev.getsentry.net'].includes(window.location.host); + return isBrowser() && ['sentry.io', 'dev.getsentry.net'].includes(window.location.host); }