Skip to content

feat: SSR useAuth to remove hydration flicker [FaaFyfxR9WAQrL7FcAgEHJvztd8cVMxvjHRS55rw1nwH] - #522

Closed
waterWang wants to merge 1 commit into
BountyOnChain:mainfrom
waterWang:feat/ssr-useauth-400
Closed

feat: SSR useAuth to remove hydration flicker [FaaFyfxR9WAQrL7FcAgEHJvztd8cVMxvjHRS55rw1nwH]#522
waterWang wants to merge 1 commit into
BountyOnChain:mainfrom
waterWang:feat/ssr-useauth-400

Conversation

@waterWang

Copy link
Copy Markdown

Summary

Implements Issue #400 — server-side rendering useAuth to remove the hydration flicker where SSR-rendered pages briefly show "Connect wallet to view" before the correct auth state is restored from localStorage.

Problem

useAuth initial state was unknown until localStorage was read post-mount. SSR-rendered pages showed the wrong state on first paint, then flickered to the correct state after hydration.

Changes

  1. lib/get-auth-status.server.ts (new) — server-only function that reads the auth-token cookie via Next.js cookies() API. Can only be imported from Server Components.

  2. lib/api.ts — added setAuthCookie() and clearAuthCookie() that sync the auth token to a non-HttpOnly cookie alongside localStorage. The cookie is set/cleared whenever the token is stored or removed.

  3. components/WalletContext.tsx — added initialHasToken prop to WalletProvider. The new hasToken field is exposed through useWallet() and is seeded from the server cookie so the first paint shows the correct state.

  4. app/layout.tsx — reads the cookie via getAuthStatusFromCookie() and passes initialHasToken to WalletProvider.

Acceptance Criteria

  • ✅ First paint shows correct connection state (no flash)

Closes #400

Problem: useAuth initial state was unknown until localStorage was read
post-mount, causing SSR-rendered pages to briefly show 'Connect wallet
to view' before the correct state appeared.

Changes:
- Add lib/get-auth-status.server.ts — server-only function that reads
  the auth-token cookie via Next.js cookies() API
- Add setAuthCookie() / clearAuthCookie() to lib/api.ts — syncs the
  auth token to a non-HttpOnly cookie alongside localStorage
- Update WalletProvider to accept initialHasToken prop, seeding the
  initial render with the server-detected token state
- Update RootLayout to read the cookie and pass initialHasToken

First paint now shows the correct connection state immediately.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[S-B] Server-side rendering useAuth to remove hydration flicker

2 participants