From 56010752c20faee2597f0cefaaf9dba9d81be84b Mon Sep 17 00:00:00 2001 From: Petr Heinz Date: Thu, 8 Aug 2024 18:51:04 +0200 Subject: [PATCH] [browser] Improve compatibility of getCurrentContext (#122) --- packages/browser/src/browser.ts | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/browser/src/browser.ts b/packages/browser/src/browser.ts index da2a27b..1a73528 100644 --- a/packages/browser/src/browser.ts +++ b/packages/browser/src/browser.ts @@ -67,18 +67,22 @@ export class Browser extends Base { } protected getCurrentContext(): Context { - return { - context: { - url: window.location.href, - user_locale: (navigator as any).userLanguage || navigator.language, - user_agent: navigator.userAgent, - device_pixel_ratio: window.devicePixelRatio, - screen_width: window.screen.width, - screen_height: window.screen.height, - window_width: window.innerWidth, - window_height: window.innerHeight, - }, - }; + const context: Context = {}; + + if (typeof window !== "undefined") { + context.url = window.location?.href; + context.device_pixel_ratio = window.devicePixelRatio; + context.screen_width = window.screen?.width; + context.screen_height = window.screen?.height; + context.window_width = window.innerWidth; + context.window_height = window.innerHeight; + } + if (typeof navigator !== "undefined") { + context.user_locale = (navigator as any).userLanguage || navigator.language; + context.user_agent = navigator.userAgent; + } + + return context; } private configureFlushOnPageLeave(): void {