Skip to content

Conversation

@nicknisi
Copy link
Member

Summary

  • Fixed hydration mismatch errors when using eagerAuth with SSR
  • Properly separated server and client snapshots in tokenStore to ensure consistent SSR behavior
  • Maintained full eagerAuth performance benefits while eliminating hydration warnings

Problem

The tokenStore's eagerAuth feature was causing hydration mismatches because:

  • Server couldn't read cookies (returned undefined)
  • Client could read cookies (returned actual token)
  • This caused React hydration to fail when the server and client HTML didn't match

Solution

Implemented proper SSR handling using React's useSyncExternalStore pattern:

  1. Server snapshot always returns undefined - ensures consistent server rendering
  2. Client only reads cookies after checking typeof window - prevents server-side cookie access
  3. Hydration uses server snapshot first - React's useSyncExternalStore uses getServerSnapshot during hydration to match server HTML

Technical Details

The fix leverages React's built-in SSR support in useSyncExternalStore:

  • During SSR: Uses getServerSnapshot() → returns undefined
  • During hydration: Still uses getServerSnapshot() → matches server
  • After hydration: Switches to getSnapshot() → returns actual token

This ensures the HTML matches during hydration while still providing immediate token access post-hydration.

Testing

  • Verified no hydration warnings in Next.js App Router
  • Confirmed tokens are still eagerly loaded and immediately available
  • Tested with Convex integration - fetchAccessToken works correctly
  • Existing test suite passes

Impact

  • No API changes - useAccessToken hook interface unchanged
  • Performance maintained - Tokens still eagerly loaded from cookies
  • SSR compatible - Proper hydration with server-side rendering
  • Framework agnostic - Works with any SSR framework using React 18+

Fixes hydration issues reported when using AuthKit with SSR frameworks.

@nicknisi nicknisi requested a review from a team as a code owner September 16, 2025 02:25
@nicknisi nicknisi requested a review from cmatheson September 16, 2025 02:25
- Update test expectations to match wrapped error messages
- Provide valid JWT tokens in mock sessions to prevent decoding errors
- Tests now expect 'Failed to refresh session:' prefix in error messages
@nicknisi nicknisi requested a review from nholden September 16, 2025 03:29
Copy link
Contributor

@cmatheson cmatheson left a comment

Choose a reason for hiding this comment

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

neat!

@nicknisi nicknisi merged commit 988f2e1 into main Sep 16, 2025
4 checks passed
@nicknisi nicknisi deleted the nicknisi/eager-auth-fixes branch September 16, 2025 15:49
@nicknisi nicknisi mentioned this pull request Sep 16, 2025
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.

3 participants