Skip to content

Commit 0a59e12

Browse files
feat(clerk-js): Introduce Clerk.client.clearCache() (#1545)
* feat(clerk-js): Introduce Clerk.client.clearCache() The clearCache method clears the internal token cache for each session of the current client. Fixes #1525 * feat(clerk-js): Use timer.unref() Teach ClerkJS Headless not to block the exit of the event loop when used in Node environments.
1 parent ea95525 commit 0a59e12

File tree

6 files changed

+26
-1
lines changed

6 files changed

+26
-1
lines changed

.changeset/swift-rivers-behave.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clerk/clerk-js': minor
3+
'@clerk/types': patch
4+
---
5+
6+
Introduce Clerk.client.clearCache() method

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

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ export class Client extends BaseResource implements ClientResource {
6060
});
6161
}
6262

63+
clearCache(): void {
64+
return this.sessions.forEach(s => s.clearCache());
65+
}
66+
6367
fromJSON(data: ClientJSON | null): this {
6468
if (data) {
6569
this.id = data.id;

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

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ export class Session extends BaseResource implements SessionResource {
6565
});
6666
};
6767

68+
clearCache = (): void => {
69+
return SessionTokenCache.clear();
70+
};
71+
6872
getToken: GetToken = async (options?: GetTokenOptions): Promise<string | null> => {
6973
return runWithExponentialBackOff(() => this._getToken(options), {
7074
shouldRetry: (error: unknown, currentIteration: number) => !is4xxError(error) && currentIteration < 4,

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@ export class TokenCacheKey {
5050
const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
5151
const cache = new Map<string, TokenCacheValue>();
5252

53+
let timer: ReturnType<typeof setTimeout>;
54+
5355
const size = () => {
5456
return cache.size;
5557
};
5658

5759
const clear = () => {
60+
clearTimeout(timer);
5861
cache.clear();
5962
};
6063

@@ -83,7 +86,13 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
8386

8487
// Mutate cached value and set expirations
8588
value.expiresIn = expiresIn;
86-
setTimeout(deleteKey, expiresIn * 1000);
89+
timer = setTimeout(deleteKey, expiresIn * 1000);
90+
91+
// Teach ClerkJS not to block the exit of the event loop when used in Node environments.
92+
// More info at https://nodejs.org/api/timers.html#timeoutunref
93+
if (typeof timer.unref === 'function') {
94+
timer.unref();
95+
}
8796
})
8897
.catch(() => {
8998
deleteKey();

packages/types/src/client.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface ClientResource extends ClerkResource {
1111
isNew: () => boolean;
1212
create: () => Promise<ClientResource>;
1313
destroy: () => Promise<void>;
14+
clearCache: () => void;
1415
lastActiveSessionId: string | null;
1516
createdAt: Date | null;
1617
updatedAt: Date | null;

packages/types/src/session.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface SessionResource extends ClerkResource {
1818
remove: () => Promise<SessionResource>;
1919
touch: () => Promise<SessionResource>;
2020
getToken: GetToken;
21+
clearCache: () => void;
2122
createdAt: Date;
2223
updatedAt: Date;
2324
}

0 commit comments

Comments
 (0)