Skip to content

Commit 8f9481c

Browse files
committed
fix(clerk-react): Make getOrCreateInstance handle both SSR and CSR instantiation
1 parent 221919c commit 8f9481c

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

packages/react/src/isomorphicClerk.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,16 @@ export default class IsomorphicClerk {
6464
static #instance: IsomorphicClerk;
6565

6666
static getOrCreateInstance(params: NewIsomorphicClerkParams) {
67-
if (!this.#instance) {
67+
// During SSR: a new instance should be created for every request
68+
// During CSR: use the cached instance for the whole lifetime of the app
69+
// This method should be idempotent in both scenarios
70+
if (!inClientSide() || !this.#instance) {
6871
this.#instance = new IsomorphicClerk(params);
6972
}
7073
return this.#instance;
7174
}
7275

7376
constructor(params: NewIsomorphicClerkParams) {
74-
if (IsomorphicClerk.#instance) {
75-
throw new Error('An IsomorphicClerk instance already exists. Use IsomorphicClerk.getOrCreateInstance instead');
76-
}
77-
7877
const { Clerk = null, frontendApi, options = {} } = params || {};
7978
this.frontendApi = frontendApi;
8079
this.options = options;

0 commit comments

Comments
 (0)