11import { inBrowser } from '@clerk/shared/browser' ;
22import { deriveState } from '@clerk/shared/deriveState' ;
3- import { loadClerkJsScript , type LoadClerkJsScriptOptions } from '@clerk/shared/loadClerkJsScript' ;
3+ import { loadClerkJsScript , type LoadClerkJsScriptOptions , loadClerkUiScript } from '@clerk/shared/loadClerkJsScript' ;
44import type {
55 Clerk ,
66 ClientResource ,
@@ -10,11 +10,18 @@ import type {
1010 Resources ,
1111 Without ,
1212} from '@clerk/shared/types' ;
13+ import type { ClerkUiConstructor } from '@clerk/shared/ui' ;
1314import type { Plugin } from 'vue' ;
1415import { computed , ref , shallowRef , triggerRef } from 'vue' ;
1516
1617import { ClerkInjectionKey } from './keys' ;
1718
19+ declare global {
20+ interface Window {
21+ __unstable_ClerkUiCtor ?: ClerkUiConstructor ;
22+ }
23+ }
24+
1825export type PluginOptions = Without < IsomorphicClerkOptions , 'domain' | 'proxyUrl' > &
1926 MultiDomainAndOrProxy & {
2027 initialState ?: InitialState ;
@@ -66,24 +73,42 @@ export const clerkPlugin: Plugin<[PluginOptions]> = {
6673 // We need this check for SSR apps like Nuxt as it will try to run this code on the server
6774 // and loadClerkJsScript contains browser-specific code
6875 if ( inBrowser ( ) ) {
69- void loadClerkJsScript ( options ) . then ( async ( ) => {
70- if ( ! window . Clerk ) {
71- throw new Error ( 'Failed to download latest ClerkJS. Contact support@clerk.com.' ) ;
72- }
76+ void ( async ( ) => {
77+ try {
78+ const clerkPromise = loadClerkJsScript ( options ) ;
79+ const clerkUiCtorPromise = ( async ( ) => {
80+ await loadClerkUiScript ( options ) ;
81+ if ( ! window . __unstable_ClerkUiCtor ) {
82+ throw new Error ( 'Failed to download latest Clerk UI. Contact support@clerk.com.' ) ;
83+ }
84+ return window . __unstable_ClerkUiCtor ;
85+ } ) ( ) ;
86+
87+ await clerkPromise ;
7388
74- clerk . value = window . Clerk ;
75- await window . Clerk . load ( options ) ;
76- loaded . value = true ;
89+ if ( ! window . Clerk ) {
90+ throw new Error ( 'Failed to download latest ClerkJS. Contact support@clerk.com.' ) ;
91+ }
7792
78- clerk . value . addListener ( payload => {
79- resources . value = payload ;
80- } ) ;
93+ clerk . value = window . Clerk ;
94+ await window . Clerk . load ( { ... options , clerkUiCtor : clerkUiCtorPromise } ) ;
95+ loaded . value = true ;
8196
82- // When Clerk updates its state internally, Vue's reactivity system doesn't detect
83- // the change since it's an external object being mutated. triggerRef() forces Vue
84- // to re-evaluate all dependencies regardless of how the value was changed.
85- triggerRef ( clerk ) ;
86- } ) ;
97+ if ( clerk . value ) {
98+ clerk . value . addListener ( payload => {
99+ resources . value = payload ;
100+ } ) ;
101+
102+ // When Clerk updates its state internally, Vue's reactivity system doesn't detect
103+ // the change since it's an external object being mutated. triggerRef() forces Vue
104+ // to re-evaluate all dependencies regardless of how the value was changed.
105+ triggerRef ( clerk ) ;
106+ }
107+ } catch ( err ) {
108+ const error = err as Error ;
109+ console . error ( error . stack || error . message || error ) ;
110+ }
111+ } ) ( ) ;
87112 }
88113
89114 const derivedState = computed ( ( ) => deriveState ( loaded . value , resources . value , initialState ) ) ;
0 commit comments