diff --git a/src/SignalR/clients/ts/signalr/src/HttpConnection.ts b/src/SignalR/clients/ts/signalr/src/HttpConnection.ts index aef719f33a4ed31530e39af5716e1879db705cec..91dcd1536770098954184b3fe1d37184988b242c 100644 --- a/src/SignalR/clients/ts/signalr/src/HttpConnection.ts +++ b/src/SignalR/clients/ts/signalr/src/HttpConnection.ts @@ -533,7 +533,7 @@ export class HttpConnection implements IConnection { return url; } - if (!Platform.isBrowser || !window.document) { + if (!Platform.isBrowser) { throw new Error(`Cannot resolve '${url}'.`); } diff --git a/src/SignalR/clients/ts/signalr/src/HubConnection.ts b/src/SignalR/clients/ts/signalr/src/HubConnection.ts index cd8071de489880d05fc7fe0c30dede2c2a2023b9..3f1363374c7912ee075d196f3be436050d3ba128 100644 --- a/src/SignalR/clients/ts/signalr/src/HubConnection.ts +++ b/src/SignalR/clients/ts/signalr/src/HubConnection.ts @@ -180,10 +180,8 @@ export class HubConnection { await this._startInternal(); if (Platform.isBrowser) { - if (document) { - // Log when the browser freezes the tab so users know why their connection unexpectedly stopped working - document.addEventListener("freeze", this._freezeEventListener); - } + // Log when the browser freezes the tab so users know why their connection unexpectedly stopped working + window.document.addEventListener("freeze", this._freezeEventListener); } this._connectionState = HubConnectionState.Connected; @@ -734,9 +732,7 @@ export class HubConnection { this._connectionStarted = false; if (Platform.isBrowser) { - if (document) { - document.removeEventListener("freeze", this._freezeEventListener); - } + window.document.removeEventListener("freeze", this._freezeEventListener); } try { diff --git a/src/SignalR/clients/ts/signalr/src/Utils.ts b/src/SignalR/clients/ts/signalr/src/Utils.ts index 389c3f67cf4178752f2f9901a9f4eb12605116ed..90eb0cc59ced72ed63f838dbad9b1a493a7f6b67 100644 --- a/src/SignalR/clients/ts/signalr/src/Utils.ts +++ b/src/SignalR/clients/ts/signalr/src/Utils.ts @@ -35,16 +35,25 @@ export class Arg { /** @private */ export class Platform { + // react-native has a window but no document so we should check both public static get isBrowser(): boolean { - return typeof window === "object"; + return typeof window === "object" && typeof window.document === "object"; } + // WebWorkers don't have a window object so the isBrowser check would fail public static get isWebWorker(): boolean { return typeof self === "object" && "importScripts" in self; } + // react-native has a window but no document + static get isReactNative(): boolean { + return typeof window === "object" && typeof window.document === "undefined"; + } + + // Node apps shouldn't have a window object, but WebWorkers don't either + // so we need to check for both WebWorker and window public static get isNode(): boolean { - return !this.isBrowser && !this.isWebWorker; + return !this.isBrowser && !this.isWebWorker && !this.isReactNative; } }