Skip to content

Commit acf21f0

Browse files
committed
feat(react): Do not hotload clerkUi if clerkUiCtor was passed
1 parent ac34168 commit acf21f0

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

integration/templates/express-vite/src/client/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ document.addEventListener('DOMContentLoaded', async function () {
77
const clerk = new Clerk(publishableKey);
88

99
await clerk.load({
10-
clerkUiCtor: Promise.resolve(ClerkUi),
10+
clerkUiCtor: ClerkUi,
1111
});
1212

1313
if (clerk.isSignedIn) {

packages/clerk-js/src/core/clerk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ export class Clerk implements ClerkInterface {
461461

462462
// Initialize ClerkUi if it was provided
463463
if (this.#options.clerkUiCtor) {
464-
this.#clerkUi = this.#options.clerkUiCtor.then(
464+
this.#clerkUi = Promise.resolve(this.#options.clerkUiCtor).then(
465465
ClerkUI =>
466466
new ClerkUI(
467467
() => this,

packages/react/src/isomorphicClerk.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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,10 +455,10 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
456455
}
457456

458457
try {
459-
const clerkPromise = this.loadClerkJsEntryChunk();
460-
const clerkUiCtor = this.loadClerkUiEntryChunk();
461-
await clerkPromise;
458+
const clerkPromise = this.getClerkJsEntryChunk();
459+
const clerkUiCtor = this.getClerkUiEntryChunk();
462460

461+
global.Clerk = await clerkPromise;
463462
if (!global.Clerk) {
464463
// TODO @nikos: somehow throw if clerk ui failed to load but it was not headless
465464
throw new Error('Failed to download latest ClerkJS. Contact [email protected].');
@@ -480,9 +479,9 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
480479
}
481480
}
482481

483-
private async loadClerkJsEntryChunk() {
482+
private async getClerkJsEntryChunk(): Promise<HeadlessBrowserClerk | BrowserClerk | undefined> {
484483
// Hotload bundle
485-
if (!this.Clerk && !__BUILD_DISABLE_RHC__) {
484+
if (!this.options.Clerk && !__BUILD_DISABLE_RHC__) {
486485
// the UMD script sets the global.Clerk instance
487486
await loadClerkJsScript({
488487
...this.options,
@@ -494,25 +493,32 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
494493
}
495494

496495
// 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;
496+
if (this.options.Clerk) {
497+
return isConstructor<BrowserClerkConstructor | HeadlessBrowserClerkConstructor>(this.options.Clerk)
498+
? new this.options.Clerk(this.#publishableKey, { proxyUrl: this.proxyUrl, domain: this.domain })
499+
: this.options.Clerk;
501500
}
502-
return global.Clerk;
501+
502+
return;
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

packages/shared/src/types/clerk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ export type ClerkOptions = ClerkOptionsNavigation &
10561056
/**
10571057
* Clerk UI entrypoint.
10581058
*/
1059-
clerkUiCtor?: Promise<ClerkUiConstructor>;
1059+
clerkUiCtor?: ClerkUiConstructor | Promise<ClerkUiConstructor>;
10601060
/**
10611061
* Optional object to style your components. Will only affect [Clerk Components](https://clerk.com/docs/reference/components/overview) and not [Account Portal](https://clerk.com/docs/guides/customizing-clerk/account-portal) pages.
10621062
*/

0 commit comments

Comments
 (0)