Back to Clerk

`getToken` now uses proactive background refresh

packages/upgrade/src/versions/core-3/changes/gettoken-stale-while-revalidate.md

latest1.3 KB
Original Source

session.getToken() now implements a stale-while-revalidate pattern that improves performance by returning cached tokens immediately while refreshing them in the background when they're close to expiration.

How it works

  1. When a token is within 15 seconds of expiration, getToken() returns the valid cached token immediately
  2. A background refresh is triggered automatically to fetch a fresh token
  3. Subsequent calls receive the new token once the background refresh completes

Benefits

  • Reduced latency: No more waiting for token refresh on every call near expiration
  • Better user experience: API calls proceed immediately with valid (though expiring) tokens
  • Automatic refresh: Fresh tokens are ready before the old ones expire

Cross-tab synchronization

Token updates are automatically synchronized across browser tabs using BroadcastChannel. When one tab refreshes a token, other tabs receive the update automatically.

Example

js
// Token is cached and valid but expiring in 10 seconds
// Core 2 behavior: Would block and fetch new token
// Core 3 behavior: Returns cached token immediately, refreshes in background
const token = await session.getToken();

Compatibility

This is a transparent improvement - no code changes are required. Your existing getToken() calls benefit automatically.