Skip to content

fix(useWindowDimensions): a bare react-native re-export is not SSR-safe - #26

Merged
thinkclay merged 1 commit into
mainfrom
fix/hydration-window-dimensions
Aug 31, 2026
Merged

thinkclay merged 1 commit into
mainfrom
fix/hydration-window-dimensions

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

One layer of Scaffald/SaaS#625.

useWindowDimensions was literally export { useWindowDimensions } from 'react-native'. React Native Web reads Dimensions directly with no getServerSnapshot, so an SSR page renders at the server's idea of the viewport and the client's hydration pass renders at the real one. Every style derived from the width then differs, React discards the tree, and the page is re-rendered on the client — the whole cost of SSR paid and then thrown away.

useResponsive, in the file next door, already solved this: a shared useSyncExternalStore whose getServerSnapshot returns a stable 1280×900. React uses that snapshot for the server render and the client's hydration render before switching to the live value, so the first paint agrees by construction and the real viewport arrives as an ordinary state update.

This moves useWindowDimensions onto the same store. The file keeps its path and re-exports, so all ~18 call sites are fixed without touching them.

Measured, with a control

The check is a comparison, not a single reading — a logged-out public page is the control, because if that fails too the cause is not auth-gated at all:

before   logged out: clean    logged in: MISMATCH at DrawerContent's `flex: 1, width: '100%'`
after    logged out: clean    logged in: MISMATCH at ScaffaldLogo

This removes one real layer and exposes the next. I am not claiming it closes #625.

The remaining mismatch is the useId gradient in the logo, which is what #625 originally described. That report was not wrong — it was describing a different layer from the one that had since become dominant. Fixing the drawer's width simply let React get far enough to notice the logo again.

How the location was found

The component stack, not inference. My first probe truncated the error to 200 chars and hid the stack; a second one that kept it showed the mismatch was on a width-derived div inside DrawerContent, nowhere near the logo. An earlier version of the same probe also used a loose /hydrat/i filter that matched 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 opposite verdict.

Both mistakes are written into scripts/audit/smoke-hydration.mjs (in the app repo) so the next person does not repeat them.

🤖 Generated with Claude Code

`useWindowDimensions` was `export { useWindowDimensions } from 'react-native'`.
React Native Web reads `Dimensions` directly, with no `getServerSnapshot`, so
an SSR page renders at the server's idea of the viewport and the client's
hydration pass renders at the real one. Every style derived from the width
then differs, React discards the tree, and the page is re-rendered on the
client — the whole cost of SSR paid and then thrown away (#625).

`useResponsive` right next to it already solved this: a shared
`useSyncExternalStore` whose `getServerSnapshot` returns a stable 1280×900.
React uses that snapshot for the server render AND the client's hydration
render, then switches to the live value — so the first paint agrees by
construction and the real viewport arrives as an ordinary state update.

This moves `useWindowDimensions` onto the same store. The file keeps its path
and re-exports, so all ~18 call sites are fixed without touching them.

`scale` and `fontScale` still come from `Dimensions`: neither varies between
server and client in a way that moves layout, and nothing here branches on
them.

─── What this does and does not fix ───────────────────────────────────────

Measured on /dashboard with a session, against a logged-out public page as
the control:

  before   DrawerContent's `flex: 1, width: '100%'` div        MISMATCH
  after    that div hydrates cleanly; ScaffaldLogo now reports MISMATCH

So this removes one real layer and exposes the next. The remaining mismatch is
the `useId` gradient in the logo, which is what #625 originally described —
that report was not wrong, it was describing a different layer from the one
that had since become dominant. #625 stays open.

Co-Authored-By: Claude Opus 5 <[email protected]>
@thinkclay
thinkclay merged commit a720d0b into main Aug 31, 2026
@thinkclay
thinkclay deleted the fix/hydration-window-dimensions branch August 31, 2026 18:20
thinkclay added a commit to Scaffald/SaaS that referenced this pull request Aug 31, 2026
…ate (#669)

Part of #625 — one layer of it, not the whole thing.

`useWindowDimensions` was a bare re-export from react-native, which reads
`Dimensions` with no `getServerSnapshot`. The server rendered at its idea of
the viewport, the client hydrated at the real one, and every style derived
from the width diverged. `DrawerContent` computes `isSmall = width < 1024`
from it, so the app shell mismatched on `flex: 1, width: '100%'` and React
discarded the tree on every authenticated page.

Scaffald/ui#26 moves the hook onto the same hydration-safe store as
`useResponsive`, fixing all ~18 call sites without touching them.

scripts/audit/smoke-hydration.mjs is the guard, and it is a COMPARISON: a
logged-out public page is the control. Without it there is no way to tell
"auth-gated mismatch" from "this app never hydrates cleanly".

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

The remaining mismatch is the logo's `useId` gradient — which is what #625
described in the first place. That report was not wrong; it named a different
layer from the one that had since become dominant, and fixing the width let
React get far enough to notice the logo again. #625 stays open.

Two things the script now documents, because both are mistakes I made while
writing it:

  - a loose /hydrat/i filter matches react-native-web's harmless startup log
    `{rootTag: #root, hydrate: true}`, which made a clean logged-out load look
    like a failure and nearly produced the opposite verdict
  - truncating the error hides the component stack, which is the only part
    that says WHERE — it is what showed the fault was the drawer's width and
    not the logo

Submodule pointer: b0ab20c..a720d0b

Co-authored-by: Claude Opus 5 <[email protected]>
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.

1 participant