Skip to content

Commit 9ad7eb4

Browse files
committed
feat(clerk-js): Enhance BroadcastChannel logging
1 parent 79c8dc7 commit 9ad7eb4

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,18 @@ interface SessionTokenEvent {
131131
traceId: string;
132132
}
133133

134+
const generateTabId = (): string => {
135+
return Math.random().toString(36).slice(2);
136+
};
137+
134138
/**
135139
* Creates an in-memory token cache with optional BroadcastChannel synchronization across tabs.
136140
* Automatically manages token expiration and cleanup via scheduled timeouts.
137141
* BroadcastChannel support is enabled only in the channel build variant.
138142
*/
139143
const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
140144
const cache = new Map<string, TokenCacheValue>();
145+
const tabId = generateTabId();
141146

142147
let broadcastChannel: BroadcastChannel | null = null;
143148

@@ -213,6 +218,7 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
213218
expectedTokenId,
214219
organizationId: data.organizationId,
215220
receivedTokenId: data.tokenId,
221+
tabId,
216222
template: data.template,
217223
traceId: data.traceId,
218224
},
@@ -227,7 +233,7 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
227233
} catch (error) {
228234
debugLogger.warn(
229235
'Failed to parse token from broadcast, skipping cache update',
230-
{ error, tokenId: data.tokenId, traceId: data.traceId },
236+
{ error, tabId, tokenId: data.tokenId, traceId: data.traceId },
231237
'tokenCache',
232238
);
233239
return;
@@ -238,7 +244,7 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
238244
if (!iat || !exp) {
239245
debugLogger.warn(
240246
'Token missing iat/exp claim, skipping cache update',
241-
{ tokenId: data.tokenId, traceId: data.traceId },
247+
{ tabId, tokenId: data.tokenId, traceId: data.traceId },
242248
'tokenCache',
243249
);
244250
return;
@@ -252,7 +258,7 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
252258
if (existingIat && existingIat >= iat) {
253259
debugLogger.debug(
254260
'Ignoring older token broadcast',
255-
{ existingIat, incomingIat: iat, tokenId: data.tokenId, traceId: data.traceId },
261+
{ existingIat, incomingIat: iat, tabId, tokenId: data.tokenId, traceId: data.traceId },
256262
'tokenCache',
257263
);
258264
return;
@@ -261,7 +267,7 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
261267
} catch (error) {
262268
debugLogger.warn(
263269
'Existing entry compare failed; proceeding with broadcast update',
264-
{ error, tokenId: data.tokenId, traceId: data.traceId },
270+
{ error, tabId, tokenId: data.tokenId, traceId: data.traceId },
265271
'tokenCache',
266272
);
267273
}
@@ -271,6 +277,7 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
271277
{
272278
iat,
273279
organizationId: data.organizationId,
280+
tabId,
274281
template: data.template,
275282
tokenId: data.tokenId,
276283
traceId: data.traceId,
@@ -362,6 +369,7 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
362369
{
363370
organizationId,
364371
sessionId,
372+
tabId,
365373
template,
366374
tokenId: entry.tokenId,
367375
traceId,

0 commit comments

Comments
 (0)