Skip to content

fix(ssr): render each page for the device that asked for it — mobile CLS 0.98 → 0.0 - #785

Merged
thinkclay merged 3 commits into
mainfrom
feat/782-ssr-viewport
Sep 17, 2026
Merged

thinkclay merged 3 commits into
mainfrom
feat/782-ssr-viewport

Conversation

@thinkclay

@thinkclay thinkclay commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

What

The server now renders each page for the device that asked for it. Fixes #782, and carries #774's last acceptance box (the budget ratchet).

  • @scaffald/ui (feat(useResponsive): take the server snapshot from a ServerViewportProvider ui#29, merged, pointer bumped here): useResponsive and useWindowDimensions take their server snapshot from a new ServerViewportProvider instead of a constant 1280×900. React uses the same snapshot for the hydration render, so the first client render still matches the server; after hydration the measured width takes over as before. Default unchanged.
  • Route loaders classify the request (apps/scaffald/utils/server-viewport.ts): Sec-CH-UA-Mobile: ?1 → phone, else a coarse UA sniff → phone / tablet / desktop, mapped to a snapshot inside each breakpoint band (390 / 820 / 1280). The three loaders that existed spread it into their data; /, /terms, /privacy, /support, /contact and /auth gain a loader whose only job is the hint.
  • The root layout reads the hint through useServerViewportHint and mounts the provider above everything. Loaders are the only server-side code that sees the request and useLoaderData() only answers for its own route, so the hook reads the same two places useLoaderData does — expo-router's ServerDataLoaderContext on the server, __EXPO_ROUTER_LOADER_DATA__ on the client's first render — once, in a state initialiser. The expo-router/build/... import is not a public path; its shape is typed and pinned by SDK 56, so an upgrade that moves it fails the typecheck rather than silently rendering desktop for everyone.
  • .lighthouserc.json budgets set from measured medians plus ~15% headroom, still warn; the ratchet rule (lower when a PR beats a number, never raise) is in the workflow comment.

Numbers

Production export, expo serve, Lighthouse 12.8 mobile preset, median of 3. Same rig as #783.

/jobs, mobile preset perf FCP LCP SI TBT CLS
main before #783 0.10 2708 94 328 33 794 1682 0.981
after #783 0.27 3155 3155 38 139 1602 0.981
this PR 0.53 3005 3005 22 482 1550 0.000

Server HTML for a phone UA now carries the phone layout (hamburger, no search input); a desktop UA gets the desktop one. Both carry the 11 job cards from #783.

Hydration

Three loads per page per device on the production build (/jobs, /jobs/<slug>, /users/<slug>, /, /terms × phone 390 with an iPhone UA, tablet 800 with an iPad UA, desktop 1440): zero "Hydration failed" errors. On main's build the same probe at phone width re-laid out every page.

One thing the probe did surface that is not this PR: /jobs/<slug> and /users/<slug> throw React #418 (the hydration-failed error, minified) once per load on main's production build too, at every width, anonymous. /jobs, / and /terms do not. Filed as #786 with the evidence.

Limits, stated

  • A desktop browser at a narrow window (say 800px) still gets the desktop snapshot — the UA cannot see the window — and re-lays out on hydration as before. Phones and tablets are what the search funnel is made of, and a wrong guess costs exactly what every visitor paid before this PR.
  • Routes without a loader keep the 1280 default. Every public and marketing route has one now; protected routes render behind a session and can adopt viewportLoader as they come up.

Verification

  • @scaffald/ui: 3 new server-render tests (default → desktop; 390 provided → isMobile, useWindowDimensions 390; undefined → default) plus the existing 16. tsc, biome clean.
  • App: 5 tests for the classifier (client hint, iPhone/Android/iPad/Android-tablet/desktop/Googlebot UAs, no headers, snapshot bands, Request). scaffald-app typecheck green, biome clean, pnpm prepush green.

🤖 Generated with Claude Code

thinkclay and others added 3 commits September 16, 2026 21:43
…80px 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]>
…er for useResponsive

Part of #782.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Part of #782 (carries #774's last acceptance box).

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.

SSR renders every page as a 1280px desktop, so phones re-lay out the whole page on hydration (CLS 0.98) and React regenerates the tree

1 participant