Skip to content

Conversation

@jacekradko
Copy link
Member

@jacekradko jacekradko commented Nov 26, 2025

Description

This PR implements a stale-while-revalidate (SWR) pattern for session tokens in getToken(). Instead of blocking on token refresh when a token is close to expiration, we return the cached token immediately and trigger a background refresh.

Fixes: USER-4087

Dashboard testing PR: https://github.com/clerk/dashboard/pull/7990

Screenshot 2026-01-15 at 9 38 16 AM

How It Works

Token TTL Timeline (token expires at T=0)
─────────────────────────────────────────────────────────────────────►
                                                                    T=0
                                                                 (expires)

│←─────── Fresh zone ───────→│←── SWR zone ──→│←─ Danger ─→│
         needsRefresh=false      needsRefresh=true    force sync fetch
         (return cached)         (return cached +     (delete from cache)
                                  background refresh)

         > threshold             5s - threshold        < 5s

Key Behaviors

  1. Fresh zone (TTL > threshold): Return cached token, no refresh needed
  2. SWR zone (5s < TTL < threshold): Return cached token immediately, trigger background refresh
  3. Danger zone (TTL < 5s): Force synchronous fetch (poller may not get to it in time)

Configuration

The backgroundRefreshThreshold option (formerly leewayInSeconds) controls when background refresh is triggered:

// Default: refresh starts 15s before expiration
const token = await session.getToken();

// Custom: refresh starts 30s before expiration
const token = await session.getToken({ backgroundRefreshThreshold: 30 });

// Minimum is 5s (the poller interval)
const token = await session.getToken({ backgroundRefreshThreshold: 10 });

Additional Options

  • refreshIfStale (internal): When true, forces a blocking fetch instead of SWR behavior. Useful as an escape hatch when you absolutely need a fresh token and can wait for it.

Implementation Details

Token Cache (tokenCache.ts)

  • get() returns { entry, needsRefresh } instead of just the entry
  • needsRefresh signals when background refresh should occur
  • Tokens < 5s from expiration are deleted from cache (forces sync fetch)
  • Supports cross-tab synchronization via BroadcastChannel

Session (Session.ts)

  • #refreshTokenInBackground(): Triggers refresh without blocking, doesn't cache the pending promise so concurrent calls still get the stale token
  • resolvedToken on cache entries enables synchronous reads (avoids microtask overhead)
  • Background refresh deduplication via #backgroundRefreshInProgress Set

AuthCookieService

  • Uses default SWR behavior (no refreshIfStale)
  • refreshTokenOnFocus returns cached token immediately to update cookie before SWR/similar libraries refetch

Breaking Changes (Core 3)

  1. Renamed option: leewayInSecondsbackgroundRefreshThreshold
  2. Lower minimum: 5s instead of 15s (aligns with poller interval)
  3. SWR behavior: Tokens in the threshold window are returned immediately instead of blocking

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Summary by CodeRabbit

  • New Features

    • Session tokens now use stale-while-revalidate: cached tokens returned immediately while a non-blocking background refresh runs; refresh deduplication and cross-tab sync improved.
    • getToken options updated: leewayInSeconds → backgroundRefreshThreshold (min 5s) and new refreshIfStale flag; cache lookups now indicate needsRefresh and expose resolved tokens; poller interval surfaced (5s).
  • Documentation

    • Added migration and behavior guides for the new refresh semantics and timing.
  • Tests

    • Expanded tests for SWR, resolution timing, expiration/leeway edge cases, and multi-session isolation.

✏️ Tip: You can customize this high-level summary in your review settings.

@changeset-bot
Copy link

changeset-bot bot commented Nov 26, 2025

🦋 Changeset detected

Latest commit: c58d1f4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 20 packages
Name Type
@clerk/clerk-js Minor
@clerk/shared Minor
@clerk/chrome-extension Patch
@clerk/expo Patch
@clerk/agent-toolkit Patch
@clerk/astro Patch
@clerk/backend Patch
@clerk/expo-passkeys Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/localizations Patch
@clerk/msw Patch
@clerk/nextjs Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/react Patch
@clerk/tanstack-react-start Patch
@clerk/testing Patch
@clerk/ui Patch
@clerk/vue Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 26, 2025

📝 Walkthrough

Walkthrough

Implements stale‑while‑revalidate for session tokens. TokenCache.get now returns a TokenCacheGetResult with entry and needsRefresh; TokenCacheEntry gained resolvedToken. backgroundRefreshThreshold replaces leewayInSeconds (minimum tied to exported POLLER_INTERVAL_IN_MS, 5000 ms). Session added a private static #backgroundRefreshInProgress Set and private helpers for token resolver creation, fetching, dispatching events, and non‑blocking background refreshes; GetTokenOptions now includes backgroundRefreshThreshold and refreshIfStale (with template moved to the end). Tests, docs, and a changeset were updated accordingly.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'feat(clerk-js): Stale-while-revalidate session token' clearly and specifically describes the main feature added: implementing stale-while-revalidate behavior for session tokens in clerk-js.
Linked Issues check ✅ Passed The PR successfully addresses all objectives from USER-4087: returns cached tokens within leeway window, triggers background refresh without blocking, avoids forced latency, and implements stale-while-revalidate pattern as required.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing stale-while-revalidate: tokenCache modifications, Session token refresh logic, GetTokenOptions API updates, tests, documentation, and export constant. No unrelated changes detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.



📜 Recent review details

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between e8b375f and c58d1f4.

📒 Files selected for processing (2)
  • packages/clerk-js/src/core/resources/Session.ts
  • packages/clerk-js/src/core/tokenCache.ts
🧰 Additional context used
📓 Path-based instructions (10)
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

All code must pass ESLint checks with the project's configuration

Files:

  • packages/clerk-js/src/core/resources/Session.ts
  • packages/clerk-js/src/core/tokenCache.ts
**/*.{js,jsx,ts,tsx,json,md,yml,yaml}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Use Prettier for consistent code formatting

Files:

  • packages/clerk-js/src/core/resources/Session.ts
  • packages/clerk-js/src/core/tokenCache.ts
packages/**/src/**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

TypeScript is required for all packages

Files:

  • packages/clerk-js/src/core/resources/Session.ts
  • packages/clerk-js/src/core/tokenCache.ts
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Follow established naming conventions (PascalCase for components, camelCase for variables)

Files:

  • packages/clerk-js/src/core/resources/Session.ts
  • packages/clerk-js/src/core/tokenCache.ts
packages/**/src/**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

packages/**/src/**/*.{ts,tsx,js,jsx}: Maintain comprehensive JSDoc comments for public APIs
Use tree-shaking friendly exports
Validate all inputs and sanitize outputs
All public APIs must be documented with JSDoc
Use dynamic imports for optional features
Provide meaningful error messages to developers
Include error recovery suggestions where applicable
Log errors appropriately for debugging
Lazy load components and features when possible
Implement proper caching strategies
Use efficient data structures and algorithms
Implement proper logging with different levels

Files:

  • packages/clerk-js/src/core/resources/Session.ts
  • packages/clerk-js/src/core/tokenCache.ts
**/*.ts?(x)

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Use proper TypeScript error types

Files:

  • packages/clerk-js/src/core/resources/Session.ts
  • packages/clerk-js/src/core/tokenCache.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/typescript.mdc)

**/*.{ts,tsx}: Always define explicit return types for functions, especially public APIs
Use proper type annotations for variables and parameters where inference isn't clear
Avoid any type - prefer unknown when type is uncertain, then narrow with type guards
Implement type guards for unknown types using the pattern function isType(value: unknown): value is Type
Use interface for object shapes that might be extended
Use type for unions, primitives, and computed types
Prefer readonly properties for immutable data structures
Use private for internal implementation details in classes
Use protected for inheritance hierarchies
Use public explicitly for clarity in public APIs
Use mixins for shared behavior across unrelated classes in TypeScript
Use generic constraints with bounded type parameters like <T extends { id: string }>
Use utility types like Omit, Partial, and Pick for data transformation instead of manual type construction
Use discriminated unions instead of boolean flags for state management and API responses
Use mapped types for transforming object types
Use conditional types for type-level logic
Leverage template literal types for string manipulation at the type level
Use ES6 imports/exports consistently
Use default exports sparingly, prefer named exports
Document functions with JSDoc comments including @param, @returns, @throws, and @example tags
Create custom error classes that extend Error for specific error types
Use the Result pattern for error handling instead of throwing exceptions
Use optional chaining (?.) and nullish coalescing (??) operators for safe property access
Let TypeScript infer obvious types to reduce verbosity
Use const assertions with as const for literal types
Use satisfies operator for type checking without widening types
Declare readonly arrays and objects for immutable data structures
Use spread operator and array spread for immutable updates instead of mutations
Use lazy loading for large types...

Files:

  • packages/clerk-js/src/core/resources/Session.ts
  • packages/clerk-js/src/core/tokenCache.ts
**/*.{js,ts,jsx,tsx}

📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)

Use ESLint with custom configurations tailored for different package types

Files:

  • packages/clerk-js/src/core/resources/Session.ts
  • packages/clerk-js/src/core/tokenCache.ts
**/*.{js,ts,jsx,tsx,json,md,yml,yaml}

📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)

Use Prettier for code formatting across all packages

Files:

  • packages/clerk-js/src/core/resources/Session.ts
  • packages/clerk-js/src/core/tokenCache.ts
**/*

⚙️ CodeRabbit configuration file

If there are no tests added or modified as part of the PR, please suggest that tests be added to cover the changes.

**/*: Only comment on issues that would block merging, ignore minor or stylistic concerns.
Restrict feedback to errors, security risks, or functionality-breaking problems.
Do not post comments on code style, formatting, or non-critical improvements.
Keep reviews short: flag only issues that make the PR unsafe to merge.
Group similar issues into a single comment instead of posting multiple notes.
Skip repetition: if a pattern repeats, mention it once at a summary level only.
Do not add general suggestions, focus strictly on merge-blocking concerns.
If there are no critical problems, respond with minimal approval (e.g., 'Looks good'). Do not add additional review.
Avoid line-by-line commentary unless it highlights a critical bug or security hole.
Highlight only issues that could cause runtime errors, data loss, or severe maintainability issues.
Ignore minor optimization opportunities, focus solely on correctness and safety.
Provide a top-level summary of critical blockers rather than detailed per-line notes.
Comment only when the issue must be resolved before merge, otherwise remain silent.
When in doubt, err on the side of fewer comments, brevity and blocking issues only.
Avoid posting any refactoring issues.

Files:

  • packages/clerk-js/src/core/resources/Session.ts
  • packages/clerk-js/src/core/tokenCache.ts
🧬 Code graph analysis (2)
packages/clerk-js/src/core/resources/Session.ts (6)
packages/clerk-js/src/core/tokenCache.ts (1)
  • SessionTokenCache (452-452)
packages/clerk-js/src/core/events.ts (2)
  • eventBus (32-32)
  • events (7-15)
packages/clerk-js/src/core/resources/User.ts (1)
  • path (107-109)
packages/shared/src/telemetry/throttler.ts (1)
  • Record (109-119)
packages/clerk-js/src/core/resources/Token.ts (1)
  • Token (7-55)
packages/shared/src/errors/missingExpiredTokenError.ts (1)
  • MissingExpiredTokenError (16-45)
packages/clerk-js/src/core/tokenCache.ts (1)
packages/clerk-js/src/core/auth/SessionCookiePoller.ts (1)
  • POLLER_INTERVAL_IN_MS (7-7)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (25)
  • GitHub Check: Integration Tests (quickstart, chrome, 15)
  • GitHub Check: Integration Tests (react-router, chrome)
  • GitHub Check: Integration Tests (nextjs, chrome, 15)
  • GitHub Check: Integration Tests (machine, chrome)
  • GitHub Check: Integration Tests (nextjs, chrome, 16, RQ)
  • GitHub Check: Integration Tests (machine, chrome, RQ)
  • GitHub Check: Integration Tests (billing, chrome, RQ)
  • GitHub Check: Integration Tests (nuxt, chrome)
  • GitHub Check: Integration Tests (custom, chrome)
  • GitHub Check: Integration Tests (quickstart, chrome, 16)
  • GitHub Check: Integration Tests (nextjs, chrome, 16)
  • GitHub Check: Integration Tests (billing, chrome)
  • GitHub Check: Integration Tests (astro, chrome)
  • GitHub Check: Integration Tests (vue, chrome)
  • GitHub Check: Integration Tests (handshake:staging, chrome)
  • GitHub Check: Integration Tests (handshake, chrome)
  • GitHub Check: Integration Tests (tanstack-react-start, chrome)
  • GitHub Check: Integration Tests (sessions:staging, chrome)
  • GitHub Check: Integration Tests (generic, chrome)
  • GitHub Check: Integration Tests (ap-flows, chrome)
  • GitHub Check: Integration Tests (sessions, chrome)
  • GitHub Check: Integration Tests (express, chrome)
  • GitHub Check: Integration Tests (localhost, chrome)
  • GitHub Check: semgrep-cloud-platform/scan
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (6)
packages/clerk-js/src/core/tokenCache.ts (2)

203-238: LGTM!

The SWR implementation correctly handles the three token freshness zones: fresh tokens are returned as-is, tokens within the refresh threshold signal needsRefresh for background refresh, and tokens too close to expiration (< 5s) force synchronous refresh by returning undefined.


366-370: LGTM!

Populating resolvedToken after the promise resolves enables synchronous reads and avoids microtask overhead for already-resolved tokens.

packages/clerk-js/src/core/resources/Session.ts (4)

47-51: LGTM!

Using a static Set for deduplication ensures that background refreshes are deduplicated across all Session instances, which is correct since tokenIds are globally unique.


366-368: LGTM!

The validation correctly prevents the background refresh threshold from exceeding the session token lifespan (60s), and appropriately excludes JWT templates which may have different lifespans.


377-396: LGTM!

The SWR flow correctly handles both cases: the poller (refreshIfStale=true) gets a blocking fetch to ensure freshness, while normal calls return the cached token immediately and trigger a non-blocking background refresh.


451-488: LGTM!

The background refresh correctly:

  1. Deduplicates concurrent refreshes via the static Set
  2. Doesn't cache the pending promise, allowing concurrent calls to use the stale token
  3. Only updates the cache on success, preventing partial states
  4. Logs errors without propagating since callers already have a valid (stale) token

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@vercel
Copy link

vercel bot commented Nov 26, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
clerk-js-sandbox Ready Ready Preview, Comment Jan 15, 2026 4:13pm

Review with Vercel Agent

@jacekradko jacekradko marked this pull request as ready for review December 1, 2025 17:23
const tokenResolver = Token.create(path, params, false);

// Cache the promise immediately to prevent concurrent calls from triggering duplicate requests
SessionTokenCache.set({ tokenId, tokenResolver });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will update the tokenResolver during the background refresh. As per the separate comment, this is not a problem during this getToken call, but wont this be a problem for any subsequent getToken calls?

I haven't verified this, so it's just my reading of the code, but I saw no test cases for this which might also be nice to add.

Say I have two queries using getToken, both triggered from components that render together. The first getToken call triggers a background refresh, which sets the tokenResolver to a promise. This first getToken returns the value immediately as it awaits the previous tokenResolver.

The second call to getToken runs immediately after, does not call a background refresh since one is in progress, but it does read the value via await tokenResolver which is now the promise for the background refresh, because of this, the second getToken call does not SWR correctly even if it should?

Since we might have to touch tokenResolver etc to fix this, this might (or might not!) be a good time to tackle something related too. Awaiting already finished promises still always resolve in a microtask at the end of the JS frame, which is inefficient, so we'll want some way to read the value synchronously after the fetch has finished. Just mentioning that in case this happens to align nicely with a fix for the above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch. Indeed, concurrent calls while refreshing would behave incorrectly as we were awaiting the new in-flight token resolver instead of returning the stale value. To address this, we no longer cache the tokenResolver running in the background until it succeeds, unlike the normal fetch case where it's cached immediately to dedupe requests. We also added a resolvedToken value to the cache so it can be read synchronously without awaiting the resolver, which avoids the microtask overhead you mentioned.

b3f14aa

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also expanded the test coverage of the SWR behavior

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 2, 2025

Open in StackBlitz

@clerk/agent-toolkit

npm i https://pkg.pr.new/@clerk/agent-toolkit@7317

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@7317

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@7317

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@7317

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@7317

@clerk/dev-cli

npm i https://pkg.pr.new/@clerk/dev-cli@7317

@clerk/expo

npm i https://pkg.pr.new/@clerk/expo@7317

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@7317

@clerk/express

npm i https://pkg.pr.new/@clerk/express@7317

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@7317

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@7317

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@7317

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@7317

@clerk/react

npm i https://pkg.pr.new/@clerk/react@7317

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@7317

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@7317

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@7317

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@7317

@clerk/ui

npm i https://pkg.pr.new/@clerk/ui@7317

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@7317

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@7317

commit: c58d1f4

Copy link
Member

@Ephem Ephem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the latest changes! Using the poller for the background revalidation makes sense to me. I think there's still some torny parts to untangle, and besides the latest comments, something still feels harder than it should be here, so I wonder if we've found the correct mental model for this yet?

I have a few early thoughts, let's chat!


if (expiresSoon) {
// Token expired or dangerously close to expiration - force synchronous refresh
if (remainingTtl <= POLLER_INTERVAL_IN_MS / 1000) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default LEEWAY was 10s, and SYNC_LEEWAY was 5s, so if I'm reading this correctly, we used to sync fetch when less than 15s was remaining? Now this value is 5s?

I wonder if 5s is enough considering latency and all other factors?

However we change this it's a breaking change so let's remember to document it properly in the changelog when we've figured it out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Before we would do a blocking fetch under the 15s, now we use the stale value when it's under 15s but more than 5 seconds. The idea there is that the poller would async fetch the token before we get to the 5s. If that doesn't occur then we would force a sync fetch.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And yes, this is a breaking change so it's going to be part of Core 3

}

return value.entry;
const effectiveLeeway = Math.max(leeway, MIN_REMAINING_TTL_IN_SECONDS);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is a bit weird. MIN_REMAINING_TTL_IN_SECONDS is 15s, and default LEEWAY is 10s, but the Math.max ensures we never go below 15s right?

If this is what we want(?), I think we should increase the LEEWAY to 15s as well, that way we document that "you can only raise this".

Thinking more on it, this PR now changes the public leewayInSeconds to only apply if you also use the internal refreshTokenOnFocus option? That seems off.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 In agreement here, this effectively forces leeway to always be equal to or greater than MIN_REMAINING_TTL_IN_SECONDS, do we want that value to be 5 or 15 going forward?

try {
const token = await this.clerk.session.getToken();
// Use refreshIfStale to fetch fresh token when cached token is within leeway period
const token = await this.clerk.session.getToken({ refreshIfStale: true });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also affects the refreshTokenOnFocus, but do we want that? Because those cases can be a race to finish setting the cookie before any external data fetching runs, it seems prudent to use the available token then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔

Comment on lines +380 to +381
// Prefer synchronous read to avoid microtask overhead when token is already resolved
const cachedToken = cacheResult.entry.resolvedToken ?? (await cacheResult.entry.tokenResolver);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned in chat, when I made the comment about this I didn't think it through fully. To get the full benefits, we'd have to make sure _getToken/getToken itself to be synchronous when data is already available (or maybe more likely add the .status and .value/.reason fields to the promise).

This likely only makes sense if/when we decide to expose this promise to the end user so they can use it though.

I see you've used resolvedToken as the way to be able to have a background refetch running while still reading the currently cached token though so this still has immediate value. That was a bit unclear at first, but I think it makes sense, will need to think some more on it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. And yeah, right now the value is key to allow for returning the stale value, while we wait for the poller to do it's thing

@nikosdouvlis nikosdouvlis force-pushed the vincent-and-the-doctor branch from d24d455 to 12a12f5 Compare December 8, 2025 17:40
- Trigger background refresh when needsRefresh is true, without
  requiring refreshIfStale option. Guarantees cache revalidation
  without relying solely on the poller.

- Add #refreshTokenInBackground() that doesn't cache pending promise,
  allowing concurrent getToken() calls to return stale token while
  refresh is in progress.

- Track in-flight background refreshes to prevent duplicate requests.

- Rename DEFAULT_LEEWAY to BACKGROUND_REFRESH_THRESHOLD_IN_SECONDS
  with clear documentation about 15s minimum and rate limiting warning.

- Add comprehensive JSDoc for leewayInSeconds option explaining
  minimum value, default, and rate limiting considerations.
Add upgrade guide entries for:
- getToken `leewayInSeconds` minimum of 15 seconds
- stale-while-revalidate pattern for session tokens
- Test refreshIfStale: true forces synchronous refresh when token is stale
- Test refreshIfStale: true with fresh token returns cached value
- Test leewayInSeconds triggers earlier background refresh
- Test minimum 15s leeway enforcement
- Test no background refresh when token has sufficient TTL
@jacekradko
Copy link
Member Author

!snapshot

@clerk-cookie
Copy link
Collaborator

Hey @jacekradko - the snapshot version command generated the following package versions:

Package Version
@clerk/agent-toolkit 0.2.9-snapshot.v20260113023037
@clerk/astro 3.0.0-snapshot.v20260113023037
@clerk/backend 3.0.0-snapshot.v20260113023037
@clerk/chrome-extension 3.0.0-snapshot.v20260113023037
@clerk/clerk-js 6.0.0-snapshot.v20260113023037
@clerk/dev-cli 1.0.0-snapshot.v20260113023037
@clerk/expo 3.0.0-snapshot.v20260113023037
@clerk/expo-passkeys 1.0.0-snapshot.v20260113023037
@clerk/express 2.0.0-snapshot.v20260113023037
@clerk/fastify 2.6.9-snapshot.v20260113023037
@clerk/localizations 4.0.0-snapshot.v20260113023037
@clerk/msw 0.0.1-snapshot.v20260113023037
@clerk/nextjs 7.0.0-snapshot.v20260113023037
@clerk/nuxt 2.0.0-snapshot.v20260113023037
@clerk/react 6.0.0-snapshot.v20260113023037
@clerk/react-router 3.0.0-snapshot.v20260113023037
@clerk/shared 4.0.0-snapshot.v20260113023037
@clerk/tanstack-react-start 1.0.0-snapshot.v20260113023037
@clerk/testing 2.0.0-snapshot.v20260113023037
@clerk/ui 1.0.0-snapshot.v20260113023037
@clerk/upgrade 2.0.0-snapshot.v20260113023037
@clerk/vue 2.0.0-snapshot.v20260113023037

Tip: Use the snippet copy button below to quickly install the required packages.
@clerk/agent-toolkit

npm i @clerk/[email protected] --save-exact

@clerk/astro

npm i @clerk/[email protected] --save-exact

@clerk/backend

npm i @clerk/[email protected] --save-exact

@clerk/chrome-extension

npm i @clerk/[email protected] --save-exact

@clerk/clerk-js

npm i @clerk/[email protected] --save-exact

@clerk/dev-cli

npm i @clerk/[email protected] --save-exact

@clerk/expo

npm i @clerk/[email protected] --save-exact

@clerk/expo-passkeys

npm i @clerk/[email protected] --save-exact

@clerk/express

npm i @clerk/[email protected] --save-exact

@clerk/fastify

npm i @clerk/[email protected] --save-exact

@clerk/localizations

npm i @clerk/[email protected] --save-exact

@clerk/msw

npm i @clerk/[email protected] --save-exact

@clerk/nextjs

npm i @clerk/[email protected] --save-exact

@clerk/nuxt

npm i @clerk/[email protected] --save-exact

@clerk/react

npm i @clerk/[email protected] --save-exact

@clerk/react-router

npm i @clerk/[email protected] --save-exact

@clerk/shared

npm i @clerk/[email protected] --save-exact

@clerk/tanstack-react-start

npm i @clerk/[email protected] --save-exact

@clerk/testing

npm i @clerk/[email protected] --save-exact

@clerk/ui

npm i @clerk/[email protected] --save-exact

@clerk/upgrade

npm i @clerk/[email protected] --save-exact

@clerk/vue

npm i @clerk/[email protected] --save-exact

- Rename option from leewayInSeconds to backgroundRefreshThreshold for clarity
- Lower minimum threshold from 15s to 5s (poller interval)
- Remove explicit refreshIfStale: false in AuthCookieService (default behavior)
- Update tests and upgrade documentation
# Conflicts:
#	packages/clerk-js/bundlewatch.config.json
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@packages/clerk-js/src/core/tokenCache.ts`:
- Line 79: The JSDoc for refreshThreshold in tokenCache.ts incorrectly states a
minimum of 15s; update the param description for refreshThreshold to reflect the
actual enforced minimum (POLLER_INTERVAL_IN_MS / 1000, i.e., 5s) or remove the
hardcoded "minimum" clause; reference the refreshThreshold param and
POLLER_INTERVAL_IN_MS constant and ensure the description matches the
Math.max(refreshThreshold, POLLER_INTERVAL_IN_MS / 1000) behavior used in the
code.
📜 Review details

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between d2cac65 and d5075a7.

📒 Files selected for processing (2)
  • packages/clerk-js/src/core/resources/__tests__/Session.test.ts
  • packages/clerk-js/src/core/tokenCache.ts
🧰 Additional context used
📓 Path-based instructions (13)
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

All code must pass ESLint checks with the project's configuration

Files:

  • packages/clerk-js/src/core/resources/__tests__/Session.test.ts
  • packages/clerk-js/src/core/tokenCache.ts
**/*.{js,jsx,ts,tsx,json,md,yml,yaml}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Use Prettier for consistent code formatting

Files:

  • packages/clerk-js/src/core/resources/__tests__/Session.test.ts
  • packages/clerk-js/src/core/tokenCache.ts
packages/**/src/**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

TypeScript is required for all packages

Files:

  • packages/clerk-js/src/core/resources/__tests__/Session.test.ts
  • packages/clerk-js/src/core/tokenCache.ts
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Follow established naming conventions (PascalCase for components, camelCase for variables)

Files:

  • packages/clerk-js/src/core/resources/__tests__/Session.test.ts
  • packages/clerk-js/src/core/tokenCache.ts
packages/**/src/**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

packages/**/src/**/*.{ts,tsx,js,jsx}: Maintain comprehensive JSDoc comments for public APIs
Use tree-shaking friendly exports
Validate all inputs and sanitize outputs
All public APIs must be documented with JSDoc
Use dynamic imports for optional features
Provide meaningful error messages to developers
Include error recovery suggestions where applicable
Log errors appropriately for debugging
Lazy load components and features when possible
Implement proper caching strategies
Use efficient data structures and algorithms
Implement proper logging with different levels

Files:

  • packages/clerk-js/src/core/resources/__tests__/Session.test.ts
  • packages/clerk-js/src/core/tokenCache.ts
**/*.{test,spec}.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

**/*.{test,spec}.{ts,tsx,js,jsx}: Unit tests are required for all new functionality
Verify proper error handling and edge cases
Include tests for all new features

Files:

  • packages/clerk-js/src/core/resources/__tests__/Session.test.ts
**/*.ts?(x)

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Use proper TypeScript error types

Files:

  • packages/clerk-js/src/core/resources/__tests__/Session.test.ts
  • packages/clerk-js/src/core/tokenCache.ts
**/*.{test,spec,e2e}.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Use real Clerk instances for integration tests

Files:

  • packages/clerk-js/src/core/resources/__tests__/Session.test.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/typescript.mdc)

**/*.{ts,tsx}: Always define explicit return types for functions, especially public APIs
Use proper type annotations for variables and parameters where inference isn't clear
Avoid any type - prefer unknown when type is uncertain, then narrow with type guards
Implement type guards for unknown types using the pattern function isType(value: unknown): value is Type
Use interface for object shapes that might be extended
Use type for unions, primitives, and computed types
Prefer readonly properties for immutable data structures
Use private for internal implementation details in classes
Use protected for inheritance hierarchies
Use public explicitly for clarity in public APIs
Use mixins for shared behavior across unrelated classes in TypeScript
Use generic constraints with bounded type parameters like <T extends { id: string }>
Use utility types like Omit, Partial, and Pick for data transformation instead of manual type construction
Use discriminated unions instead of boolean flags for state management and API responses
Use mapped types for transforming object types
Use conditional types for type-level logic
Leverage template literal types for string manipulation at the type level
Use ES6 imports/exports consistently
Use default exports sparingly, prefer named exports
Document functions with JSDoc comments including @param, @returns, @throws, and @example tags
Create custom error classes that extend Error for specific error types
Use the Result pattern for error handling instead of throwing exceptions
Use optional chaining (?.) and nullish coalescing (??) operators for safe property access
Let TypeScript infer obvious types to reduce verbosity
Use const assertions with as const for literal types
Use satisfies operator for type checking without widening types
Declare readonly arrays and objects for immutable data structures
Use spread operator and array spread for immutable updates instead of mutations
Use lazy loading for large types...

Files:

  • packages/clerk-js/src/core/resources/__tests__/Session.test.ts
  • packages/clerk-js/src/core/tokenCache.ts
**/*.test.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)

Use React Testing Library for component testing

Files:

  • packages/clerk-js/src/core/resources/__tests__/Session.test.ts
**/*.{js,ts,jsx,tsx}

📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)

Use ESLint with custom configurations tailored for different package types

Files:

  • packages/clerk-js/src/core/resources/__tests__/Session.test.ts
  • packages/clerk-js/src/core/tokenCache.ts
**/*.{js,ts,jsx,tsx,json,md,yml,yaml}

📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)

Use Prettier for code formatting across all packages

Files:

  • packages/clerk-js/src/core/resources/__tests__/Session.test.ts
  • packages/clerk-js/src/core/tokenCache.ts
**/*

⚙️ CodeRabbit configuration file

If there are no tests added or modified as part of the PR, please suggest that tests be added to cover the changes.

**/*: Only comment on issues that would block merging, ignore minor or stylistic concerns.
Restrict feedback to errors, security risks, or functionality-breaking problems.
Do not post comments on code style, formatting, or non-critical improvements.
Keep reviews short: flag only issues that make the PR unsafe to merge.
Group similar issues into a single comment instead of posting multiple notes.
Skip repetition: if a pattern repeats, mention it once at a summary level only.
Do not add general suggestions, focus strictly on merge-blocking concerns.
If there are no critical problems, respond with minimal approval (e.g., 'Looks good'). Do not add additional review.
Avoid line-by-line commentary unless it highlights a critical bug or security hole.
Highlight only issues that could cause runtime errors, data loss, or severe maintainability issues.
Ignore minor optimization opportunities, focus solely on correctness and safety.
Provide a top-level summary of critical blockers rather than detailed per-line notes.
Comment only when the issue must be resolved before merge, otherwise remain silent.
When in doubt, err on the side of fewer comments, brevity and blocking issues only.
Avoid posting any refactoring issues.

Files:

  • packages/clerk-js/src/core/resources/__tests__/Session.test.ts
  • packages/clerk-js/src/core/tokenCache.ts
🧬 Code graph analysis (1)
packages/clerk-js/src/core/resources/__tests__/Session.test.ts (3)
packages/clerk-js/src/core/tokenCache.ts (1)
  • SessionTokenCache (452-452)
packages/react/src/isomorphicClerk.ts (1)
  • session (722-728)
packages/clerk-js/src/core/resources/Session.ts (2)
  • Session (44-499)
  • token (422-433)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (25)
  • GitHub Check: Integration Tests (nextjs, chrome, 16, RQ)
  • GitHub Check: Integration Tests (quickstart, chrome, 16)
  • GitHub Check: Integration Tests (billing, chrome, RQ)
  • GitHub Check: Integration Tests (quickstart, chrome, 15)
  • GitHub Check: Integration Tests (nextjs, chrome, 16)
  • GitHub Check: Integration Tests (react-router, chrome)
  • GitHub Check: Integration Tests (sessions, chrome)
  • GitHub Check: Integration Tests (machine, chrome, RQ)
  • GitHub Check: Integration Tests (billing, chrome)
  • GitHub Check: Integration Tests (custom, chrome)
  • GitHub Check: Integration Tests (nextjs, chrome, 15)
  • GitHub Check: Integration Tests (machine, chrome)
  • GitHub Check: Integration Tests (astro, chrome)
  • GitHub Check: Integration Tests (vue, chrome)
  • GitHub Check: Integration Tests (nuxt, chrome)
  • GitHub Check: Integration Tests (tanstack-react-start, chrome)
  • GitHub Check: Integration Tests (handshake, chrome)
  • GitHub Check: Integration Tests (ap-flows, chrome)
  • GitHub Check: Integration Tests (sessions:staging, chrome)
  • GitHub Check: Integration Tests (handshake:staging, chrome)
  • GitHub Check: Integration Tests (express, chrome)
  • GitHub Check: Integration Tests (generic, chrome)
  • GitHub Check: Integration Tests (localhost, chrome)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: semgrep-cloud-platform/scan
🔇 Additional comments (1)
packages/clerk-js/src/core/resources/__tests__/Session.test.ts (1)

422-737: Comprehensive SWR test coverage.

The test suite thoroughly covers the stale-while-revalidate behavior including edge cases for background refresh failures, retry logic, threshold configuration, and the refreshIfStale option. Timer calculations are correct relative to the mock JWT's 60s TTL.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.

* @param cacheKeyJSON - Object containing tokenId and optional audience to identify the cached entry
* @param leeway - Optional seconds before expiration to treat token as expired (default: 10s). Combined with 5s sync leeway.
* @returns The cached TokenCacheEntry if found and valid, undefined otherwise
* @param refreshThreshold - Seconds before expiration to trigger background refresh (default: 15s, minimum: 15s).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

JSDoc minimum value is incorrect.

The documentation states minimum: 15s but the code at line 232 enforces Math.max(refreshThreshold, POLLER_INTERVAL_IN_MS / 1000), which is 5s (the poller interval). The test at line 676-708 in Session.test.ts confirms thresholds below 15s work correctly.

Suggested fix
-   * `@param` refreshThreshold - Seconds before expiration to trigger background refresh (default: 15s, minimum: 15s).
+   * `@param` refreshThreshold - Seconds before expiration to trigger background refresh (default: 15s, minimum: 5s).
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
* @param refreshThreshold - Seconds before expiration to trigger background refresh (default: 15s, minimum: 15s).
* `@param` refreshThreshold - Seconds before expiration to trigger background refresh (default: 15s, minimum: 5s).
🤖 Prompt for AI Agents
In `@packages/clerk-js/src/core/tokenCache.ts` at line 79, The JSDoc for
refreshThreshold in tokenCache.ts incorrectly states a minimum of 15s; update
the param description for refreshThreshold to reflect the actual enforced
minimum (POLLER_INTERVAL_IN_MS / 1000, i.e., 5s) or remove the hardcoded
"minimum" clause; reference the refreshThreshold param and POLLER_INTERVAL_IN_MS
constant and ensure the description matches the Math.max(refreshThreshold,
POLLER_INTERVAL_IN_MS / 1000) behavior used in the code.

@jacekradko
Copy link
Member Author

!snapshot

@clerk-cookie
Copy link
Collaborator

Hey @jacekradko - the snapshot version command generated the following package versions:

Package Version
@clerk/agent-toolkit 0.2.9-snapshot.v20260115162133
@clerk/astro 3.0.0-snapshot.v20260115162133
@clerk/backend 3.0.0-snapshot.v20260115162133
@clerk/chrome-extension 3.0.0-snapshot.v20260115162133
@clerk/clerk-js 6.0.0-snapshot.v20260115162133
@clerk/dev-cli 1.0.0-snapshot.v20260115162133
@clerk/expo 3.0.0-snapshot.v20260115162133
@clerk/expo-passkeys 1.0.0-snapshot.v20260115162133
@clerk/express 2.0.0-snapshot.v20260115162133
@clerk/fastify 2.6.9-snapshot.v20260115162133
@clerk/localizations 4.0.0-snapshot.v20260115162133
@clerk/msw 0.0.1-snapshot.v20260115162133
@clerk/nextjs 7.0.0-snapshot.v20260115162133
@clerk/nuxt 2.0.0-snapshot.v20260115162133
@clerk/react 6.0.0-snapshot.v20260115162133
@clerk/react-router 3.0.0-snapshot.v20260115162133
@clerk/shared 4.0.0-snapshot.v20260115162133
@clerk/tanstack-react-start 1.0.0-snapshot.v20260115162133
@clerk/testing 2.0.0-snapshot.v20260115162133
@clerk/ui 1.0.0-snapshot.v20260115162133
@clerk/upgrade 2.0.0-snapshot.v20260115162133
@clerk/vue 2.0.0-snapshot.v20260115162133

Tip: Use the snippet copy button below to quickly install the required packages.
@clerk/agent-toolkit

npm i @clerk/[email protected] --save-exact

@clerk/astro

npm i @clerk/[email protected] --save-exact

@clerk/backend

npm i @clerk/[email protected] --save-exact

@clerk/chrome-extension

npm i @clerk/[email protected] --save-exact

@clerk/clerk-js

npm i @clerk/[email protected] --save-exact

@clerk/dev-cli

npm i @clerk/[email protected] --save-exact

@clerk/expo

npm i @clerk/[email protected] --save-exact

@clerk/expo-passkeys

npm i @clerk/[email protected] --save-exact

@clerk/express

npm i @clerk/[email protected] --save-exact

@clerk/fastify

npm i @clerk/[email protected] --save-exact

@clerk/localizations

npm i @clerk/[email protected] --save-exact

@clerk/msw

npm i @clerk/[email protected] --save-exact

@clerk/nextjs

npm i @clerk/[email protected] --save-exact

@clerk/nuxt

npm i @clerk/[email protected] --save-exact

@clerk/react

npm i @clerk/[email protected] --save-exact

@clerk/react-router

npm i @clerk/[email protected] --save-exact

@clerk/shared

npm i @clerk/[email protected] --save-exact

@clerk/tanstack-react-start

npm i @clerk/[email protected] --save-exact

@clerk/testing

npm i @clerk/[email protected] --save-exact

@clerk/ui

npm i @clerk/[email protected] --save-exact

@clerk/upgrade

npm i @clerk/[email protected] --save-exact

@clerk/vue

npm i @clerk/[email protected] --save-exact

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants