@@ -250,12 +250,11 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
250250 }
251251
252252 constructor ( options : IsomorphicClerkOptions ) {
253- const { Clerk = null , publishableKey } = options || { } ;
254- this . #publishableKey = publishableKey ;
253+ this . #publishableKey = options ?. publishableKey ;
255254 this . #proxyUrl = options ?. proxyUrl ;
256255 this . #domain = options ?. domain ;
257256 this . options = options ;
258- this . Clerk = Clerk ;
257+ this . Clerk = options ?. Clerk || null ;
259258 this . mode = inBrowser ( ) ? 'browser' : 'server' ;
260259 this . #stateProxy = new StateProxy ( this ) ;
261260
@@ -266,7 +265,7 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
266265 this . #eventBus. prioritizedOn ( clerkEvents . Status , status => ( this . #status = status ) ) ;
267266
268267 if ( this . #publishableKey) {
269- void this . loadClerkEntryChunks ( ) ;
268+ void this . getEntryChunks ( ) ;
270269 }
271270 }
272271
@@ -436,7 +435,7 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
436435 } ) ;
437436 }
438437
439- async loadClerkEntryChunks ( ) : Promise < void > {
438+ async getEntryChunks ( ) : Promise < void > {
440439 if ( this . mode !== 'browser' || this . loaded ) {
441440 return ;
442441 }
@@ -456,21 +455,15 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
456455 }
457456
458457 try {
459- const clerkPromise = this . loadClerkJsEntryChunk ( ) ;
460- const clerkUiCtor = this . loadClerkUiEntryChunk ( ) ;
461- await clerkPromise ;
458+ const clerkUiCtor = this . getClerkUiEntryChunk ( ) ;
459+ const clerk = await this . getClerkJsEntryChunk ( ) ;
462460
463- if ( ! global . Clerk ) {
464- // TODO @nikos : somehow throw if clerk ui failed to load but it was not headless
465- throw new Error ( 'Failed to download latest ClerkJS. Contact [email protected] .' ) ; 461+ if ( ! clerk . loaded ) {
462+ this . beforeLoad ( clerk ) ;
463+ await clerk . load ( { ... this . options , clerkUiCtor } ) ;
466464 }
467-
468- if ( ! global . Clerk . loaded ) {
469- this . beforeLoad ( global . Clerk ) ;
470- await global . Clerk . load ( { ...this . options , clerkUiCtor } ) ;
471- }
472- if ( global . Clerk . loaded ) {
473- this . replayInterceptedInvocations ( global . Clerk ) ;
465+ if ( clerk . loaded ) {
466+ this . replayInterceptedInvocations ( clerk ) ;
474467 }
475468 } catch ( err ) {
476469 const error = err as Error ;
@@ -480,10 +473,11 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
480473 }
481474 }
482475
483- private async loadClerkJsEntryChunk ( ) {
476+ private async getClerkJsEntryChunk ( ) : Promise < HeadlessBrowserClerk | BrowserClerk > {
484477 // Hotload bundle
485- if ( ! this . Clerk && ! __BUILD_DISABLE_RHC__ ) {
478+ if ( ! this . options . Clerk && ! __BUILD_DISABLE_RHC__ ) {
486479 // the UMD script sets the global.Clerk instance
480+ // we do not want to await here as we
487481 await loadClerkJsScript ( {
488482 ...this . options ,
489483 publishableKey : this . #publishableKey,
@@ -494,25 +488,37 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
494488 }
495489
496490 // Otherwise, set global.Clerk to the bundled ctor or instance
497- if ( this . Clerk ) {
498- global . Clerk = isConstructor < BrowserClerkConstructor | HeadlessBrowserClerkConstructor > ( this . Clerk )
499- ? new this . Clerk ( this . #publishableKey, { proxyUrl : this . proxyUrl , domain : this . domain } )
500- : this . Clerk ;
491+ if ( this . options . Clerk ) {
492+ global . Clerk = isConstructor < BrowserClerkConstructor | HeadlessBrowserClerkConstructor > ( this . options . Clerk )
493+ ? new this . options . Clerk ( this . #publishableKey, { proxyUrl : this . proxyUrl , domain : this . domain } )
494+ : this . options . Clerk ;
501495 }
496+
497+ if ( ! global . Clerk ) {
498+ // TODO @nikos : somehow throw if clerk ui failed to load but it was not headless
499+ throw new Error ( 'Failed to download latest ClerkJS. Contact [email protected] .' ) ; 500+ }
501+
502502 return global . Clerk ;
503503 }
504504
505- private async loadClerkUiEntryChunk ( ) {
505+ private async getClerkUiEntryChunk ( ) : Promise < ClerkUiConstructor > {
506+ if ( this . options . clerkUiCtor ) {
507+ return this . options . clerkUiCtor ;
508+ }
509+
506510 await loadClerkUiScript ( {
507511 ...this . options ,
508512 publishableKey : this . #publishableKey,
509513 proxyUrl : this . proxyUrl ,
510514 domain : this . domain ,
511515 nonce : this . options . nonce ,
512516 } ) ;
517+
513518 if ( ! global . __unstable_ClerkUiCtor ) {
514519 throw new Error ( 'Failed to download latest Clerk UI. Contact [email protected] .' ) ; 515520 }
521+
516522 return global . __unstable_ClerkUiCtor ;
517523 }
518524
0 commit comments