Skip to content

Commit 465f929

Browse files
Use window.document for React-Native (#38340)
1 parent 9dd048a commit 465f929

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

src/SignalR/clients/ts/signalr/src/HttpConnection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ export class HttpConnection implements IConnection {
533533
return url;
534534
}
535535

536-
if (!Platform.isBrowser || !window.document) {
536+
if (!Platform.isBrowser) {
537537
throw new Error(`Cannot resolve '${url}'.`);
538538
}
539539

src/SignalR/clients/ts/signalr/src/HubConnection.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,8 @@ export class HubConnection {
180180
await this._startInternal();
181181

182182
if (Platform.isBrowser) {
183-
if (document) {
184-
// Log when the browser freezes the tab so users know why their connection unexpectedly stopped working
185-
document.addEventListener("freeze", this._freezeEventListener);
186-
}
183+
// Log when the browser freezes the tab so users know why their connection unexpectedly stopped working
184+
window.document.addEventListener("freeze", this._freezeEventListener);
187185
}
188186

189187
this._connectionState = HubConnectionState.Connected;
@@ -734,9 +732,7 @@ export class HubConnection {
734732
this._connectionStarted = false;
735733

736734
if (Platform.isBrowser) {
737-
if (document) {
738-
document.removeEventListener("freeze", this._freezeEventListener);
739-
}
735+
window.document.removeEventListener("freeze", this._freezeEventListener);
740736
}
741737

742738
try {

src/SignalR/clients/ts/signalr/src/Utils.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,25 @@ export class Arg {
3535

3636
/** @private */
3737
export class Platform {
38+
// react-native has a window but no document so we should check both
3839
public static get isBrowser(): boolean {
39-
return typeof window === "object";
40+
return typeof window === "object" && typeof window.document === "object";
4041
}
4142

43+
// WebWorkers don't have a window object so the isBrowser check would fail
4244
public static get isWebWorker(): boolean {
4345
return typeof self === "object" && "importScripts" in self;
4446
}
4547

48+
// react-native has a window but no document
49+
static get isReactNative(): boolean {
50+
return typeof window === "object" && typeof window.document === "undefined";
51+
}
52+
53+
// Node apps shouldn't have a window object, but WebWorkers don't either
54+
// so we need to check for both WebWorker and window
4655
public static get isNode(): boolean {
47-
return !this.isBrowser && !this.isWebWorker;
56+
return !this.isBrowser && !this.isWebWorker && !this.isReactNative;
4857
}
4958
}
5059

0 commit comments

Comments
 (0)