Skip to content

Commit c720c5c

Browse files
committed
feat(clerk-js): Add enhanced poller to main browser build
1 parent 2813fa4 commit c720c5c

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

.changeset/curly-dingos-pay.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clerk/clerk-js': minor
3+
---
4+
5+
Optimized session token poller to share token with other open tabs
6+

integration/tests/session-token-cache/single-session.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { createTestUtils, testAgainstRunningApps } from '../../testUtils';
1111
* token fetches in one tab are automatically broadcast and cached in other tabs,
1212
* eliminating redundant network requests.
1313
*/
14-
testAgainstRunningApps({ withEnv: [appConfigs.envs.withBroadcastChannel] })(
14+
testAgainstRunningApps({ withEnv: [appConfigs.envs.withEmailCodes] })(
1515
'MemoryTokenCache Multi-Tab Integration @generic',
1616
({ app }) => {
1717
test.describe.configure({ mode: 'serial' });

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ const generateTabId = (): string => {
138138
/**
139139
* Creates an in-memory token cache with optional BroadcastChannel synchronization across tabs.
140140
* Automatically manages token expiration and cleanup via scheduled timeouts.
141-
* BroadcastChannel support is enabled only in the channel build variant.
141+
* BroadcastChannel support is enabled whenever the environment provides it.
142142
*/
143143
const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
144144
const cache = new Map<string, TokenCacheValue>();
@@ -147,21 +147,19 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
147147
let broadcastChannel: BroadcastChannel | null = null;
148148

149149
const ensureBroadcastChannel = (): BroadcastChannel | null => {
150-
if (!__BUILD_VARIANT_CHANNEL__) {
151-
return null;
152-
}
153-
154150
if (broadcastChannel) {
155151
return broadcastChannel;
156152
}
157153

158-
if (typeof BroadcastChannel !== 'undefined') {
159-
broadcastChannel = new BroadcastChannel('clerk:session_token');
160-
broadcastChannel.addEventListener('message', (e: MessageEvent<SessionTokenEvent>) => {
161-
void handleBroadcastMessage(e);
162-
});
154+
if (typeof BroadcastChannel === 'undefined') {
155+
return null;
163156
}
164157

158+
broadcastChannel = new BroadcastChannel('clerk:session_token');
159+
broadcastChannel.addEventListener('message', (e: MessageEvent<SessionTokenEvent>) => {
160+
void handleBroadcastMessage(e);
161+
});
162+
165163
return broadcastChannel;
166164
};
167165

0 commit comments

Comments
 (0)