From 769d8851bbec786c7c96c90f8a5c1b750bda46ba Mon Sep 17 00:00:00 2001 From: nick-delirium Date: Thu, 14 Nov 2024 11:18:06 +0100 Subject: [PATCH] network proxy: prevent referring to window object on import, fixes #2742 --- networkProxy/src/beaconProxy.ts | 2 +- networkProxy/src/index.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/networkProxy/src/beaconProxy.ts b/networkProxy/src/beaconProxy.ts index 11eac6e33..7d2581c17 100644 --- a/networkProxy/src/beaconProxy.ts +++ b/networkProxy/src/beaconProxy.ts @@ -74,7 +74,7 @@ export class BeaconProxyHandler implement } export default class BeaconProxy { - public static origSendBeacon = window?.navigator?.sendBeacon + public static origSendBeacon = typeof window !== 'undefined' ? window.navigator?.sendBeacon : undefined public static hasSendBeacon() { return !!BeaconProxy.origSendBeacon diff --git a/networkProxy/src/index.ts b/networkProxy/src/index.ts index f5cd0ca68..c452eddfc 100644 --- a/networkProxy/src/index.ts +++ b/networkProxy/src/index.ts @@ -54,6 +54,7 @@ export default function createNetworkProxy( }, tokenUrlMatcher?: (url: string) => boolean, ): void { + if (!context) return; if (modules.xhr) { if (context.XMLHttpRequest) { context.XMLHttpRequest = XHRProxy.create( @@ -83,7 +84,7 @@ export default function createNetworkProxy( } } if (modules.beacon) { - if (context?.navigator?.sendBeacon) { + if (context.navigator?.sendBeacon) { context.navigator.sendBeacon = BeaconProxy.create( ignoredHeaders, setSessionTokenHeader,