Skip to content

ref(replay): Improve isBrowser check #6328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/replay/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ global.__SENTRY_REPLAY_VERSION__ = 'version:Test';

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

jest.mock('./src/util/isBrowser', () => {
return {
isBrowser: () => true,
};
});

afterEach(() => {
const hub = getCurrentHub();
if (typeof hub?.getClient !== 'function') {
Expand Down
11 changes: 5 additions & 6 deletions packages/replay/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -243,7 +242,7 @@ export class Replay implements Integration {
* PerformanceObserver, Recording, Sentry SDK, etc)
*/
start(): void {
if (!isBrowser) {
if (!isBrowser()) {
return;
}

Expand Down Expand Up @@ -309,7 +308,7 @@ export class Replay implements Integration {
* does not support a teardown
*/
stop(): void {
if (!isBrowser) {
if (!isBrowser()) {
return;
}

Expand Down
5 changes: 5 additions & 0 deletions packages/replay/src/util/isBrowser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { isNodeEnv } from '@sentry/utils';

export function isBrowser(): boolean {
return typeof window !== 'undefined' && !isNodeEnv();
}
4 changes: 2 additions & 2 deletions packages/replay/src/util/isInternal.ts
Original file line number Diff line number Diff line change
@@ -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);
}