Skip to content

Commit

Permalink
fix(clerk-js,clerk-react,types): Optimize Coinbase SDK loading
Browse files Browse the repository at this point in the history
Instead of lazy loading the Coinbase SDK in getEthereumProvider(), move loading when the SignIn/SignUp components mount. This fix avoid the Safari to block the Coinbase popup
  • Loading branch information
nikospapcom committed Dec 17, 2024
1 parent 315564b commit c5315fd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 18 deletions.
8 changes: 4 additions & 4 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ export class Clerk implements ClerkInterface {
protected internal_last_error: ClerkAPIError | null = null;
// converted to protected environment to support `updateEnvironment` type assertion
protected environment?: EnvironmentResource | null;
private __promise = createDeferredPromise();

#publishableKey = '';
#domain: DomainOrProxyUrl['domain'];
Expand All @@ -201,6 +200,7 @@ export class Clerk implements ClerkInterface {
#options: ClerkOptions = {};
#pageLifecycle: ReturnType<typeof createPageLifecycle> | null = null;
#touchThrottledUntil = 0;
#coinbaseSDKResolvers = createDeferredPromise();

public __internal_getCachedResources:
| (() => Promise<{ client: ClientJSONSnapshot | null; environment: EnvironmentJSONSnapshot | null }>)
Expand Down Expand Up @@ -1672,12 +1672,12 @@ export class Clerk implements ClerkInterface {
this.environment = environment;
}

public prefetchDependencies = async (): Promise<void> => {
private prefetchDependencies = async (): Promise<void> => {
if (this.isCoinbaseWalletEnabled) {
window.__clerk_coinbase_sdk = this.__promise.promise as Promise<typeof CoinbaseWalletSDK>;
window.__clerk_coinbase_sdk = this.#coinbaseSDKResolvers.promise as Promise<typeof CoinbaseWalletSDK>;

await import('@coinbase/wallet-sdk').then(mod => {
this.__promise.resolve(mod.CoinbaseWalletSDK);
this.#coinbaseSDKResolvers.resolve(mod.CoinbaseWalletSDK);
});
}
};
Expand Down
6 changes: 5 additions & 1 deletion packages/clerk-js/src/utils/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ export async function generateSignatureWithOKXWallet(params: GenerateSignaturePa

async function getEthereumProvider(provider: Web3Provider) {
if (provider === 'coinbase_wallet') {
const CoinbaseWalletSDK = await window.__clerk_coinbase_sdk;
const CoinbaseWalletSDK = await window.__clerk_coinbase_sdk.catch(() => null);
if (!CoinbaseWalletSDK) {
throw new Error('Coinbase Wallet SDK is not loaded');
}

const sdk = new CoinbaseWalletSDK({});
return sdk.makeWeb3Provider({ options: 'all' });
}
Expand Down
11 changes: 0 additions & 11 deletions packages/react/src/isomorphicClerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ type IsomorphicLoadedClerk = Without<
| 'client'
| '__internal_getCachedResources'
| '__internal_reloadInitialResources'
| 'prefetchDependencies'
> & {
// TODO: Align return type and parms
handleRedirectCallback: (params: HandleOAuthCallbackParams) => void;
Expand Down Expand Up @@ -174,7 +173,6 @@ type IsomorphicLoadedClerk = Without<
mountSignIn: (node: HTMLDivElement, props: SignInProps) => void;
mountUserProfile: (node: HTMLDivElement, props: UserProfileProps) => void;
mountWaitlist: (node: HTMLDivElement, props: WaitlistProps) => void;
prefetchDependencies: () => void;
client: ClientResource | undefined;
};

Expand Down Expand Up @@ -1216,13 +1214,4 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
this.premountMethodCalls.set('signOut', callback);
}
};

prefetchDependencies = async (): Promise<void> => {
const callback = () => this.clerkjs?.prefetchDependencies();
if (this.clerkjs && this.#loaded) {
return callback() as Promise<void>;
} else {
this.premountMethodCalls.set('prefetchDependencies', callback);
}
};
}
2 changes: 0 additions & 2 deletions packages/types/src/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,6 @@ export interface Clerk {
* This funtion is used to reload the initial resources (Environment/Client) from the Frontend API.
**/
__internal_reloadInitialResources: () => Promise<void>;

prefetchDependencies: () => Promise<void>;
}

export type HandleOAuthCallbackParams = TransferableOption &
Expand Down

0 comments on commit c5315fd

Please sign in to comment.