Skip to content

Commit e17c0f6

Browse files
committed
support vue
1 parent 188b5d0 commit e17c0f6

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

packages/shared/src/internal/clerk-js/encoders.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function encodeB64(input: string) {
55
// https://stackoverflow.com/questions/30106476/
66
export function decodeB64(input: string) {
77
return decodeURIComponent(
8-
global
8+
globalThis
99
.atob(input)
1010
.split('')
1111
.map(c => {

packages/vue/src/plugin.ts

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { inBrowser } from '@clerk/shared/browser';
22
import { deriveState } from '@clerk/shared/deriveState';
3-
import { loadClerkJsScript, type LoadClerkJsScriptOptions } from '@clerk/shared/loadClerkJsScript';
3+
import { loadClerkJsScript, type LoadClerkJsScriptOptions, loadClerkUiScript } from '@clerk/shared/loadClerkJsScript';
44
import 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';
1314
import type { Plugin } from 'vue';
1415
import { computed, ref, shallowRef, triggerRef } from 'vue';
1516

1617
import { ClerkInjectionKey } from './keys';
1718

19+
declare global {
20+
interface Window {
21+
__unstable_ClerkUiCtor?: ClerkUiConstructor;
22+
}
23+
}
24+
1825
export 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

Comments
 (0)