Skip to content

fix(ssr): every authenticated page fails hydration — server renders logged-out, client hydrates logged-in #625

Closed

Description

@thinkclay

Found while building an authenticated smoke harness (scripts/audit/smoke-authed.mjs, added in #624).

Every SSR page load for a logged-in user appears to fail hydration and re-render the entire tree on the client. React reports it against ScaffaldLogo, but the logo is the symptom, not the cause.

What the overlay says

Hydration failed because the server rendered HTML didn't match the client.
As a result this tree will be regenerated on the client.

packages/scf-core/assets/ScaffaldLogo.tsx (54:5)
  <Svg width={width} height={height} viewBox={viewBox} style={style}>
    <Defs>
      <LinearGradient id={gradientId}

Why the logo is not the bug

ScaffaldLogo already does the right thing — useId(), which is exactly the #582 fix, and the comment above it says so:

const gradientId = `logo-gradient-${useId().replace(/:/g, '')}`

useId() only produces matching ids across server and client if the two render trees have the same shape. Ours do not:

  • the session lives in localStorage, which the server cannot read
  • so the server renders the logged-out tree
  • the client hydrates with a session and renders the logged-in tree
  • the trees differ, the useId counters drift, and the first component with a generated id — the logo in the app shell — is where React notices

Because the logo sits in the shell, this fires on every SSR route, not one screen. And "this tree will be regenerated on the client" means the entire page is thrown away and re-rendered, which is the whole cost of SSR paid and then discarded.

Confidence, stated honestly

I reached the authenticated screens by injecting a session into localStorage rather than by logging in through the form, so I cannot fully separate "the harness causes this" from "real auth causes this".

But the mechanism is identical either way: supabase-js persists to localStorage, the SSR server has no visibility into it, and there is no cookie handoff. A real login should produce the same divergence on the next SSR navigation. Worth confirming with a real session before investing in a fix.

Why it matters beyond the console noise

  • Every authenticated page pays for SSR and then discards it — slower first interaction, and a visible flash on slower devices.
  • It masks real hydration bugs. Once a mismatch is expected on every page, the next genuine one is invisible.
  • It is very likely related to getServerSnapshot infinite-loop error spams the console on every page #438 (getServerSnapshot infinite-loop spam) — both are server/client state-divergence symptoms.

Directions worth considering

  1. Cookie-backed session so the server can render the authenticated tree — the real fix, and the largest change.
  2. Suspend the auth-dependent subtree until hydration completes, so both sides render the same placeholder shape.
  3. Give the logo a static gradient id. Cheapest, and it silences the reported error — but it only moves the mismatch to the next component with a generated id. Not a fix.

Reproducing

pnpm supa start
pnpm supa functions serve api
# start a dev server, then:
EXPO_PUBLIC_SUPABASE_ANON_KEY=... [email protected] \
SMOKE_PASSWORD=password123 node scripts/audit/smoke-authed.mjs

The overlay appears on /office/applications, /office/ats/metrics and /dashboard alike — the harness strips it so screenshots stay usable, so read the console output rather than the images.

Activity

  1. thinkclay commented on Aug 31, 2026

    @thinkclay
    ContributorAuthor

    One layer fixed (#669, d20382c1c; submodule Scaffald/ui#26, a720d0b). Leaving this open — the mismatch moved rather than disappeared.

    The uncertainty in the original report is resolved

    I flagged that I injected a session rather than logging in, so I could not separate harness from real bug. Checked: the web client uses localStorage with persistSession: true and there is no cookie handoff anywhere — no SSR code reads auth at all. The server can never see a session however it arrived, so the divergence is real.

    Confirmed by comparison, with a logged-out public page as the control:

    logged out, /           0 hydration errors
    logged in,  /dashboard  1 hydration error
    

    But the cause was not what this issue names

    The component stack put it in DrawerContent, on a flex: 1, width: '100%' div — nowhere near the logo. useWindowDimensions was a bare re-export from react-native, which reads Dimensions with no getServerSnapshot; DrawerContent computes isSmall = width < 1024 from it, so the app shell diverged on every authenticated page. ~18 files used that hook.

    Fixed by moving it onto the same useSyncExternalStore as useResponsive, whose getServerSnapshot React uses for both the server render and the hydration render.

    before  logged in: MISMATCH at DrawerContent width
    after   logged in: MISMATCH at ScaffaldLogo
    

    So this report was right, about a different layer

    The remaining mismatch is the logo's useId gradient, exactly as described here. This issue was not wrong — it named a layer that was still there underneath a second one that had become dominant. Fixing the width let React get far enough to reach the logo again.

    Next step for whoever picks this up: scripts/audit/smoke-hydration.mjs reproduces it in one command, and prints the full component stack.

    Two traps it now documents, both of which I fell into:

    • A loose /hydrat/i filter matches react-native-web's harmless {rootTag: #root, hydrate: true} startup log. That made a clean logged-out load look like a failure and nearly produced the verdict that this issue blamed the wrong thing entirely.
    • Truncating the error hides the component stack — the only part that says where.
  2. added a commit that references this issue on Sep 1, 2026
    f03f841
  3. thinkclay commented on Sep 1, 2026

    @thinkclay
    ContributorAuthor

    Two things in this issue's diagnosis turned out to be wrong, and both mattered.

    React was reporting the attribute warning, not tree regeneration. The message is "A tree hydrated but some attributes … didn't match … This won't be patched up". Hydration succeeds and the DOM matches — nothing is thrown away, so "the whole cost of SSR paid and then discarded" overstated it.

    The server does not render a logged-out tree. A plain curl /dashboard with no session returns the drawer nav with the logo inside it. The premise that the server renders logged-out and the client hydrates logged-in is not what happens.

    What was actually wrong is narrower: useId encodes the component's position in the React tree, and the id was the only thing in the whole diff.

    Position is the wrong key here regardless of the disagreement. Two logos with the same colours want the same gradient — sharing one <linearGradient> is correct output, not a collision — and two with different colours must not share one. The colours are props, so option 3 in this issue (a static id) would have been wrong rather than merely incomplete. The id now names the gradient: logo-gradient-76EAFF-239CB2.

    /dashboard             1 mismatch → 0
    /office/applications   1 mismatch → 0
    /office/ats/metrics    1 mismatch → 0
    

    smoke-hydration.mjs now reports no hydration errors in either case; it was failing its logged-in leg.

    The cause is still open as #679. I checked instead of assuming: a throwaway useId emitted alongside the fixed id still shows + _R_2qjaqb9_ against - _R_badb9d6_, so server and client really do disagree on tree position. ScaffaldLogo was the only useId consumer in the shell (packages/ui's useId is exported but used by no component), so nothing expresses it today — adding one anywhere in the shell brings it back. #679 carries the evidence and the probe technique, including the two traps that make naive probing report a false match.

    Also worth recording against the note in this issue about not being able to separate harness from real auth: it makes no difference. The divergence reproduces the same way, and it is not auth-shaped.

  4. thinkclay commented on Sep 1, 2026

    @thinkclay
    ContributorAuthor

    Correction worth recording against my previous comment.

    I said the server does not render a logged-out tree, on the strength of curl /dashboard returning the drawer nav. That observation was right, and the conclusion I drew from it was too broad.

    Continuing to verify after #678 merged turned up a second, more severe hydration failure that is still present — the tree-regeneration variant, not the attribute warning:

    Hydration failed … As a result this tree will be regenerated on the client.

    It is intermittent. Three runs of smoke-mobile-nav on worker /dashboard with identical code: fails, clean, fails. And it does not reproduce in an isolated probe matching the smoke's setup exactly — 0 failures at every width from 1440 down to 390.

    Same inputs, different outcomes. That is a race, and it means this issue's original hypothesis was closer to right than my correction gave it credit for: the session is loaded asynchronously from localStorage, so whether the first client render is logged-in depends on whether that load wins the race. When it wins, the trees genuinely differ in structure and React discards the render.

    So the accounting is:

    #678 stands on its own merits and its verification holds. But "hydration is clean" was too strong a claim from me, and #681 is the one that carries the cost this issue was originally about.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Frontendbugpriority: medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions