Skip to content

fix(auth-next-client): stabilize session reference across window focus refetches#2798

Draft
keithbro-imx wants to merge 2 commits intomainfrom
kb/fix-session-reference-stability
Draft

fix(auth-next-client): stabilize session reference across window focus refetches#2798
keithbro-imx wants to merge 2 commits intomainfrom
kb/fix-session-reference-stability

Conversation

@keithbro-imx
Copy link
Contributor

Summary

  • next-auth's SessionProvider refetches the session on every window focus (refetchOnWindowFocus defaults to true). Each refetch returns a new object reference even when the data is unchanged, causing unnecessary re-renders and effect re-runs for consumers using session in deps or as a prop. This is a known upstream issue that the maintainers won't fix.
  • Adds a reusable useStableValue hook that uses fast-json-stable-stringify to produce a deterministic, key-order-independent string from any value and returns a stable reference via useMemo. Applied in useImmutableSession so the returned session reference only changes when the data actually changes.
  • sessionRef continues to track the raw latest session for imperative use by getUser/getAccessToken -- no behavior change for those code paths.

Test plan

  • useStableValue unit tests: reference stability, value changes, null transitions, key-order independence
  • useImmutableSession integration tests: stable reference on identical refetch, new reference on accessToken change, new reference on error change, null transitions
  • All 23 tests pass
  • Manual verification: confirm no UI flicker or unnecessary re-renders on tab focus in sample app

Made with Cursor

…s refetches

next-auth's SessionProvider refetches the session on every window focus
(refetchOnWindowFocus defaults to true). Each refetch returns a new object
reference even when the data is unchanged, causing unnecessary re-renders
and effect re-runs for consumers using session in deps or as a prop.

This is a known upstream issue (nextauthjs/next-auth#3405) that the
maintainers won't fix.

Add a reusable useStableValue hook that uses fast-json-stable-stringify
to produce a deterministic, key-order-independent string from any value
and returns a stable reference via useMemo. Apply it in useImmutableSession
so the returned session reference only changes when the data actually changes.

sessionRef continues to track the raw latest session for imperative use
by getUser/getAccessToken.

Made-with: Cursor
@nx-cloud
Copy link

nx-cloud bot commented Mar 11, 2026

View your CI Pipeline Execution ↗ for commit fbe3f5e

Command Status Duration Result
nx affected -t build,lint,test ✅ Succeeded 1m 25s View ↗
nx run-many -p @imtbl/sdk,@imtbl/checkout-widge... ✅ Succeeded 17s View ↗

☁️ Nx Cloud last updated this comment at 2026-03-12 06:08:37 UTC

…oid flaky CI

createSession() calls Date.now() internally; calling it twice could
produce different timestamps if the millisecond ticks over, causing
useStableValue to see a changed object and breaking the toBe assertion.

Made-with: Cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant