Skip to content

fix(useResponsive): return a stable server snapshot - #14

Merged
thinkclay merged 1 commit into
mainfrom
fix/server-snapshot-identity
Jul 30, 2026
Merged

thinkclay merged 1 commit into
mainfrom
fix/server-snapshot-identity

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

_getServerSnapshot allocated a fresh { width: 1280, height: 900 } on every call:

function _getServerSnapshot(): Snapshot {
  return { width: 1280, height: 900 }   // new object every read
}

React requires the server snapshot to be a cached reference. A new object each read compares unequal every time, which is precisely what the "The result of getServerSnapshot should be cached to avoid an infinite loop" warning exists to catch. useResponsive backs Box/Stack/Grid, so it runs many times per server-rendered page.

Hoisted to a module constant. One line, no behaviour change.

Scope — please read before assuming this closes anything

This is a genuine contract violation and worth fixing on its own merits, but it did not resolve that warning in the consuming app. It still fires after:

  • rebuilding this package's dist
  • wiping the Metro cache, .expo, and node_modules/.cache
  • a full cold rebuild of the dev server
  • confirming both node_modules/@scaffald/ui symlinks resolve to the fixed copy

And this is the only useSyncExternalStore call site in the entire workspace. So there is at least one more source — most likely inside react-native-web or expo-router internals rather than our code.

I'd rather land the correct code than leave a known-wrong snapshot in place, but I don't want the commit message implying a fix it didn't deliver. Tracked separately in UNI-Construct.

`_getServerSnapshot` allocated a fresh `{ width: 1280, height: 900 }` on every
call. React requires the server snapshot to be a cached reference — a new
object each read means the snapshot always compares unequal, which is what the
"result of getServerSnapshot should be cached to avoid an infinite loop"
warning exists to catch. useResponsive backs Box/Stack/Grid, so this runs many
times per server-rendered page.

Scope, honestly: this is a real contract violation and worth fixing on its own
merits, but it did NOT resolve that warning in the consuming app. It still
fires after rebuilding this package, wiping the Metro/Expo caches and doing a
cold rebuild, and this is the only useSyncExternalStore call site in the
workspace — so there is another source, most likely inside react-native-web or
expo-router. Tracked separately in UNI-Construct; do not close that on this.
@thinkclay
thinkclay merged commit 043505e into main Jul 30, 2026
@thinkclay
thinkclay deleted the fix/server-snapshot-identity branch July 30, 2026 11:16
thinkclay added a commit to Scaffald/SaaS that referenced this pull request Jul 30, 2026
Bumps @scaffald/ui to pick up Scaffald/ui#14, which had been open and
unmerged for a week. `_getServerSnapshot` returned a fresh
`{ width, height }` object on every call, so React saw the snapshot change
on every read — exactly what "The result of getServerSnapshot should be
cached to avoid an infinite loop" describes. useResponsive backs
Box/Stack/Grid, so it runs many times per SSR page.

Latent-bug removal: the warning does not currently reproduce on main with
clean caches (full bisect in #438), but returning an unstable reference
from getServerSnapshot violates React's documented contract regardless,
and the apex renders server-side.

Refs #438

Co-authored-by: Claude Fable 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