From b9fc65ec9a15e32fb6eb12d53cc45b8778854be9 Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Thu, 21 Sep 2023 11:31:38 -0700 Subject: [PATCH] feat: lazy-load safe only if iframed (#7360) --- patches/@web3-react+gnosis-safe+8.2.3.patch | 15 +++++++++++++++ src/connection/eagerlyConnect.ts | 8 +++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 patches/@web3-react+gnosis-safe+8.2.3.patch diff --git a/patches/@web3-react+gnosis-safe+8.2.3.patch b/patches/@web3-react+gnosis-safe+8.2.3.patch new file mode 100644 index 0000000000..6b1bc277ec --- /dev/null +++ b/patches/@web3-react+gnosis-safe+8.2.3.patch @@ -0,0 +1,15 @@ +diff --git a/node_modules/@web3-react/gnosis-safe/dist/index.js b/node_modules/@web3-react/gnosis-safe/dist/index.js +index 015a33c..4cd7cde 100644 +--- a/node_modules/@web3-react/gnosis-safe/dist/index.js ++++ b/node_modules/@web3-react/gnosis-safe/dist/index.js +@@ -68,8 +68,8 @@ class GnosisSafe extends types_1.Connector { + if (this.eagerConnection) + return; + // kick off import early to minimize waterfalls +- const SafeAppProviderPromise = Promise.resolve().then(() => __importStar(require('@safe-global/safe-apps-provider'))).then(({ SafeAppProvider }) => SafeAppProvider); +- yield (this.eagerConnection = Promise.resolve().then(() => __importStar(require('@safe-global/safe-apps-sdk'))).then((m) => __awaiter(this, void 0, void 0, function* () { ++ const SafeAppProviderPromise = Promise.resolve().then(async () => __importStar(await import('@safe-global/safe-apps-provider'))).then(({ SafeAppProvider }) => SafeAppProvider); ++ yield (this.eagerConnection = Promise.resolve().then(async () => __importStar(await import('@safe-global/safe-apps-sdk'))).then((m) => __awaiter(this, void 0, void 0, function* () { + this.sdk = new m.default(this.options); + const safe = yield Promise.race([ + this.sdk.safe.getInfo(), diff --git a/src/connection/eagerlyConnect.ts b/src/connection/eagerlyConnect.ts index f4ad87fe3a..390845180b 100644 --- a/src/connection/eagerlyConnect.ts +++ b/src/connection/eagerlyConnect.ts @@ -20,8 +20,14 @@ async function connect(connector: Connector, type: ConnectionType) { } } -connect(gnosisSafeConnection.connector, ConnectionType.GNOSIS_SAFE) +// The Safe connector will only work from safe.global, which iframes; +// it is only necessary to try (and to load all the deps) if we are in an iframe. +if (window !== window.parent) { + connect(gnosisSafeConnection.connector, ConnectionType.GNOSIS_SAFE) +} + connect(networkConnection.connector, ConnectionType.NETWORK) + const selectedWallet = toConnectionType(localStorage.getItem(selectedWalletKey) ?? undefined) if (selectedWallet) { const selectedConnection = getConnection(selectedWallet)