Skip to content

SSR: server and client disagree on React tree position in the app shell (the cause under #625) #679

Open

Description

@thinkclay

Split out of #625, which is fixed. This is the part that is not.

What remains

#625 was reported as a hydration mismatch on ScaffaldLogo's gradient id. #678 fixed that by keying the id on the gradient's colours instead of on useId, which encodes the component's position in the React tree.

That removed the symptom. It did not remove the cause: the server and the client do not agree on tree position for an authenticated page.

Evidence

Emit a throwaway useId alongside the now-fixed id and render it into an attribute that survives to the DOM:

// in ScaffaldLogo, temporarily
const probePosition = useId()
return <Svg ... data-probe={probePosition}>

React's own hydration diff, on /dashboard with a session:

+  data-probe="_R_2qjaqb9_"     (client)
-  data-probe="_R_badb9d6_"     (server)

Two notes on technique, both of which cost time to learn:

  1. Do not read the value back from the DOM. React says "This won't be patched up", so the server's value stays in the DOM and a document.querySelector probe reads the server value on both sides and reports a false match. The +/- lines in the console message are the only place both values appear.
  2. View from react-native-web drops unknown data-* props, so a probe there emits nothing. Svg from react-native-svg forwards them, which is why the probe has to sit on an SVG element.

Why it is currently invisible

ScaffaldLogo was the only useId consumer in the authenticated shell. packages/ui/src/accessibility/useId.ts is exported from the package but used by no component in it. Nothing else in the shell depends on tree position, so the divergence has no way to express itself.

Add one useId anywhere in the shell — a form control, a labelled input, an aria-describedby — and it comes straight back, on every SSR route.

What is known and not known

Known:

  • Hydration succeeds; the DOM matches. This is the attribute warning, not tree regeneration.
  • The server does not render a logged-out tree — curl /dashboard with no session returns the drawer nav and the logo.
  • The divergence is in the root-most part of the path: across probe placements the ids share a suffix (...db9d6_ server, ...jaqb9_ client) and differ in the leading segment, which is the end of the tree-id encoding nearest the root.

Not known:

  • Which ancestor. SafeAreaProvider is worth checking first — it renders null until it has insets unless initialMetrics is supplied, which is exactly the shape of thing that changes a subtree's child count without changing DOM output. apps/scaffald/app/_layout.tsx:97 mounts it above everything.

Acceptance

  • Identify the ancestor whose rendered child count differs between server and client
  • Server and client agree on useId for a probe component mounted in the shell
  • A useId consumer can be added to the shell without reintroducing a mismatch — the regression test for this issue, since that is the condition that is currently untestable
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

    agent-ready

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions