fix(auth-next-client): stabilize session reference across window focus refetches#2798
Draft
keithbro-imx wants to merge 2 commits intomainfrom
Draft
fix(auth-next-client): stabilize session reference across window focus refetches#2798keithbro-imx wants to merge 2 commits intomainfrom
keithbro-imx wants to merge 2 commits intomainfrom
Conversation
…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
|
View your CI Pipeline Execution ↗ for commit fbe3f5e
☁️ Nx Cloud last updated this comment at |
…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
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.
Summary
SessionProviderrefetches the session on every window focus (refetchOnWindowFocusdefaults totrue). Each refetch returns a new object reference even when the data is unchanged, causing unnecessary re-renders and effect re-runs for consumers usingsessionin deps or as a prop. This is a known upstream issue that the maintainers won't fix.useStableValuehook that usesfast-json-stable-stringifyto produce a deterministic, key-order-independent string from any value and returns a stable reference viauseMemo. Applied inuseImmutableSessionso the returned session reference only changes when the data actually changes.sessionRefcontinues to track the raw latest session for imperative use bygetUser/getAccessToken-- no behavior change for those code paths.Test plan
useStableValueunit tests: reference stability, value changes, null transitions, key-order independenceuseImmutableSessionintegration tests: stable reference on identical refetch, new reference on accessToken change, new reference on error change, null transitionsMade with Cursor