Skip to content

Commit 4999302

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

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

integration/presets/astro.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ const astroNode = applicationConfig()
1010
.addScript('dev', 'pnpm dev')
1111
.addScript('build', 'pnpm build')
1212
.addScript('serve', 'pnpm preview')
13-
.addDependency('@clerk/astro', linkPackage('astro'))
14-
.addDependency('@clerk/shared', linkPackage('types'))
15-
.addDependency('@clerk/localizations', linkPackage('localizations'));
13+
.addDependency('@clerk/astro', linkPackage('astro', 'integration'))
14+
.addDependency('@clerk/shared', linkPackage('types', 'integration'))
15+
.addDependency('@clerk/localizations', linkPackage('localizations', 'integration'));
1616

1717
const astroStatic = astroNode.clone().setName('astro-hybrid').useTemplate(templates['astro-hybrid']);
1818

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: 31 additions & 25 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,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

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)