feat(clerk-js): experimental SWR initialization for faster page loads#8153
Draft
nikosdouvlis wants to merge 1 commit intomainfrom
Draft
feat(clerk-js): experimental SWR initialization for faster page loads#8153nikosdouvlis wants to merge 1 commit intomainfrom
nikosdouvlis wants to merge 1 commit intomainfrom
Conversation
…oads Clerk initialization blocks on /client (slow server roundtrip). This adds an opt-in experimental.swr flag that lets clerk-js initialize immediately from a cached client snapshot, validated by a fast token check, then silently swap to fresh /client data in the background. The flow: read cached client from localStorage, check if the __session cookie JWT is still valid (SSR apps always have one from middleware refresh). If valid, emit ready instantly with zero network. If expired, call getToken (edge-routed, fast) to validate session liveness. If revoked (4xx), discard cache and fall through to normal flow. If transient error, emit degraded with cached data. Cache is scoped by publishable key, versioned for schema safety, strips JWTs and signIn/signUp state before saving, and clears on sign-out (including cross-tab via BroadcastChannel).
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Clerk initialization blocks on the /client API response, which is a full server roundtrip (500ms-2s). For returning users, this means a loading spinner on every page load even though we already have their session data from the previous visit. Auth providers are infrastructure, and infrastructure that initializes faster wins.
What
Adds an opt-in
experimental.swrflag toClerk.load()that enables stale-while-revalidate initialization. When enabled, clerk-js:__sessioncookie JWT is still validgetToken()(edge-routed, fast) to validate session livenessThe cache is scoped by publishable key, versioned for schema safety, strips JWTs and signIn/signUp flow state before saving, and clears on sign-out (including cross-tab via BroadcastChannel).
Key design decisions
Test plan
experimental: { swr: true }, sign in, reload, verify instant load🤖 Generated with Claude Code