-
Notifications
You must be signed in to change notification settings - Fork 424
feat(clerk-js): add timer-based proactive token refresh #7612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Adds a proactive refresh mechanism that triggers token refresh before the token enters the leeway window. This prevents getToken() from blocking when tokens are about to expire. The timer fires 2 seconds before the leeway period (at 43 seconds for 60-second tokens), giving time for the background fetch to complete. If the timer doesn't fire (e.g., background tab), we fall back to the existing blocking behavior. Key changes: - Add onExpiringSoon callback to TokenCacheEntry - Schedule proactive refresh timer when caching tokens - Pass callback in Session._getToken() and #hydrateCache()
1fd4640 to
0e812a2
Compare
Why: The timer-based proactive token refresh feature (0e812a2) needed test coverage to verify getToken() behavior across all 6 timing scenarios. What changed: Added tests covering: - Before timer fires (t < 43): returns cached token instantly - After timer, fetch in progress (43 < t < 45): returns old token non-blocking - After timer, fetch complete (43 < t < 45): returns new token from cache - In leeway zone with completed fetch (t >= 45): returns new token - In leeway zone with failed fetch (t >= 45): blocks and refetches - Background tab scenario (timer didn't fire): safe fallback behavior
📝 WalkthroughWalkthroughThis change implements proactive token refresh for session tokens. A new 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
Why
When
getToken()is called and the token is within the leeway window (15 seconds before expiration), it blocks while fetching a new token. This creates latency spikes for users actively using an app.What
Implements a timer-based proactive refresh that triggers token refresh before the token enters the leeway zone:
getToken()calls await the same promise (no duplicate fetches)Key changes
onExpiringSooncallback toTokenCacheEntrySession._getToken()and#hydrateCache()Test plan
Summary by CodeRabbit
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.