From 5bff9752f56feef85e3fd65dc5cfafb8408e1740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Isnard?= Date: Tue, 9 Jan 2024 12:45:14 +0100 Subject: [PATCH] fix: If using the global Sentry, check if it has finished loading Because of lazy loading, window.Sentry can be defined as a bare-bones object, and it will be missing some functions like getCurrentHub until the Sentry SDK finishes loading. In this case, just skip the Sentry integration. --- packages/core/src/Tanker.ts | 5 ++++- packages/global-this/src/index.ts | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/core/src/Tanker.ts b/packages/core/src/Tanker.ts index 506abb0f..9a03a9e2 100644 --- a/packages/core/src/Tanker.ts +++ b/packages/core/src/Tanker.ts @@ -125,7 +125,10 @@ export class Tanker extends EventEmitter { } if (options.sentryHub === undefined) { - this._sentry = globalThis.Sentry?.getCurrentHub() || null; + // If Sentry is lazy-loaded, the globalThis.Sentry object can exist but be almost empty + // So we have to check if getCurrentHub is defined. Disable integration if it hasn't finished loading. + const getHub = globalThis.Sentry?.getCurrentHub; + this._sentry = getHub ? getHub() : null; } else { this._sentry = options.sentryHub; } diff --git a/packages/global-this/src/index.ts b/packages/global-this/src/index.ts index 58e70637..4f6c8b1a 100644 --- a/packages/global-this/src/index.ts +++ b/packages/global-this/src/index.ts @@ -2,7 +2,9 @@ import { getGlobalThis } from './global-this'; import type { Hub } from '@sentry/types'; const myGlobalThis: typeof globalThis & { - Sentry?: { getCurrentHub: () => Hub }; + Sentry?: { + getCurrentHub?: () => Hub, + }; } = getGlobalThis(); export { myGlobalThis as globalThis };