From e082cde005083275050a0a36919301fa63f1d2a5 Mon Sep 17 00:00:00 2001 From: Noah Zinsmeister Date: Mon, 2 May 2022 10:36:36 -0400 Subject: [PATCH] introduce `serverSide` --- packages/coinbase-wallet/src/index.ts | 2 +- packages/eip1193/src/index.ts | 2 +- packages/gnosis-safe/src/index.ts | 4 ++-- packages/metamask/src/index.ts | 2 +- packages/network/src/index.ts | 2 +- packages/types/src/index.ts | 4 ++++ packages/url/src/index.ts | 2 +- packages/walletconnect/src/index.ts | 2 +- 8 files changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/coinbase-wallet/src/index.ts b/packages/coinbase-wallet/src/index.ts index 6a40a44..ee2c74e 100644 --- a/packages/coinbase-wallet/src/index.ts +++ b/packages/coinbase-wallet/src/index.ts @@ -27,7 +27,7 @@ export class CoinbaseWallet extends Connector { constructor(actions: Actions, options: CoinbaseWalletSDKOptions, connectEagerly = false) { super(actions) - if (connectEagerly && typeof window === 'undefined') { + if (connectEagerly && this.serverSide) { throw new Error('connectEagerly = true is invalid for SSR, instead use the connectEagerly method in a useEffect') } diff --git a/packages/eip1193/src/index.ts b/packages/eip1193/src/index.ts index 4c8f738..0c0f195 100644 --- a/packages/eip1193/src/index.ts +++ b/packages/eip1193/src/index.ts @@ -16,7 +16,7 @@ export class EIP1193 extends Connector { constructor(actions: Actions, provider: Provider, connectEagerly = false) { super(actions) - if (connectEagerly && typeof window === 'undefined') { + if (connectEagerly && this.serverSide) { throw new Error('connectEagerly = true is invalid for SSR, instead use the connectEagerly method in a useEffect') } diff --git a/packages/gnosis-safe/src/index.ts b/packages/gnosis-safe/src/index.ts index 695c2e0..5e1fb12 100644 --- a/packages/gnosis-safe/src/index.ts +++ b/packages/gnosis-safe/src/index.ts @@ -30,7 +30,7 @@ export class GnosisSafe extends Connector { constructor(actions: Actions, connectEagerly = false, options?: Opts) { super(actions) - if (connectEagerly && typeof window === 'undefined') { + if (connectEagerly && this.serverSide) { throw new Error('connectEagerly = true is invalid for SSR, instead use the connectEagerly method in a useEffect') } @@ -41,7 +41,7 @@ export class GnosisSafe extends Connector { // check if we're in an iframe private get inIframe() { - if (typeof window === 'undefined') return false + if (this.serverSide) return false if (window !== window.parent) return true return false } diff --git a/packages/metamask/src/index.ts b/packages/metamask/src/index.ts index b29e4bf..5ed14cd 100644 --- a/packages/metamask/src/index.ts +++ b/packages/metamask/src/index.ts @@ -36,7 +36,7 @@ export class MetaMask extends Connector { constructor(actions: Actions, connectEagerly = false, options?: Parameters[0]) { super(actions) - if (connectEagerly && typeof window === 'undefined') { + if (connectEagerly && this.serverSide) { throw new Error('connectEagerly = true is invalid for SSR, instead use the connectEagerly method in a useEffect') } diff --git a/packages/network/src/index.ts b/packages/network/src/index.ts index a8859fd..2d94fae 100644 --- a/packages/network/src/index.ts +++ b/packages/network/src/index.ts @@ -41,7 +41,7 @@ export class Network extends Connector { ) { super(actions) - if (connectEagerly && typeof window === 'undefined') { + if (connectEagerly && this.serverSide) { throw new Error('connectEagerly = true is invalid for SSR, instead use the activate method in a useEffect') } diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index a6ba440..ca55b78 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -94,6 +94,10 @@ export abstract class Connector { this.actions = actions } + protected get serverSide() { + return typeof window === 'undefined' + } + /** * Attempt to initiate a connection, failing silently */ diff --git a/packages/url/src/index.ts b/packages/url/src/index.ts index fba85b1..b5befe1 100644 --- a/packages/url/src/index.ts +++ b/packages/url/src/index.ts @@ -25,7 +25,7 @@ export class Url extends Connector { constructor(actions: Actions, url: url | JsonRpcProvider, connectEagerly = false) { super(actions) - if (connectEagerly && typeof window === 'undefined') { + if (connectEagerly && this.serverSide) { throw new Error('connectEagerly = true is invalid for SSR, instead use the activate method in a useEffect') } diff --git a/packages/walletconnect/src/index.ts b/packages/walletconnect/src/index.ts index 178aab0..0621d75 100644 --- a/packages/walletconnect/src/index.ts +++ b/packages/walletconnect/src/index.ts @@ -35,7 +35,7 @@ export class WalletConnect extends Connector { constructor(actions: Actions, options: WalletConnectOptions, connectEagerly = false, treatModalCloseAsError = true) { super(actions) - if (connectEagerly && typeof window === 'undefined') { + if (connectEagerly && this.serverSide) { throw new Error('connectEagerly = true is invalid for SSR, instead use the connectEagerly method in a useEffect') }