Skip to content

feat(useResponsive): take the server snapshot from a ServerViewportProvider - #29

Merged
thinkclay merged 1 commit into
mainfrom
feat/ssr-viewport-context
Sep 17, 2026
Merged

thinkclay merged 1 commit into
mainfrom
feat/ssr-viewport-context

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

What

useResponsive (and useWindowDimensions, which shares its store) rendered every page against a constant 1280×900 server snapshot. On a phone that means the desktop layout is server-rendered and then replaced at hydration: the whole page moves (CLS ≈ 1 on every SSR page at phone width) and, because the tree changed shape during hydration, React discards the server tree and regenerates it. Measured on scaffald.com's /jobs: Scaffald/SaaS#782.

  • New ServerViewportProvider viewport={{ width, height }} + useServerViewport() + DEFAULT_SERVER_VIEWPORT, exported from the package root.
  • useResponsive and useWindowDimensions hand the provided snapshot to useSyncExternalStore as getServerSnapshot. React also uses that for the hydration render, so server and first client render agree; after hydration the measured width takes over as before.
  • Default is unchanged (1280×900), so callers that do not provide see no difference. The provider memoises by width/height so the snapshot reference is stable (the "getServerSnapshot should be cached" warning from fix(useResponsive): return a stable server snapshot #14 stays fixed).

Verification

  • 3 new tests render through react-dom/server: default → 1280 desktop; provider 390×844 → isMobile, breakpoint base, useWindowDimensions 390; provider with undefined → default. The 16 existing useResponsive tests still pass. tsc --noEmit and biome clean.
  • Consumer side (Scaffald/SaaS PR to follow): root layout provides a per-request snapshot from the route loader's UA sniff; Lighthouse mobile CLS on /jobs before/after in that PR.

🤖 Generated with Claude Code

…ovider, so the server can render the layout the device will use

The hook rendered every page against a constant 1280×900 on the server, so a
phone got the desktop layout and re-laid the whole page out at hydration
(CLS ≈ 1 on every server-rendered page at phone width), with React regenerating
the tree because its shape changed. The provider lets the app choose the
snapshot per request; React uses the same snapshot for the hydration render,
so the first client render still matches the server. Default unchanged.

useWindowDimensions shares the store and now shares the provider.

Scaffald/SaaS#782

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@thinkclay
thinkclay merged commit 771f31f into main Sep 17, 2026
@thinkclay
thinkclay deleted the feat/ssr-viewport-context branch September 17, 2026 01:44
thinkclay added a commit to Scaffald/SaaS that referenced this pull request Sep 17, 2026
…CLS 0.98 → 0.0 (#785)

* fix(ssr): render each page for the device that asked for it, not a 1280px desktop for everyone

Part of #782. Needs the @scaffald/ui pointer bump to Scaffald/ui#29.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* chore(ui): bump @scaffald/ui to Scaffald/ui#29 — ServerViewportProvider for useResponsive

Part of #782.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* ci(lighthouse): budgets from measured reality, as a ratchet

Part of #782 (carries #774's last acceptance box).

Co-Authored-By: Claude Fable 5.1 <[email protected]>

---------

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