Skip to content

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 #782

Closed

Description

@thinkclay

Found while fixing #774's LCP. The rows are now server-rendered, LCP went from 94s to 3.2s on the mobile preset — and CLS stayed at 0.981. The trace says why, and it is not the listing.

What the trace shows

Lighthouse's default preset is a 412px-wide phone. The one layout shift on /jobs (both main and the #774 branch) is at +744ms, score 0.981:

node 121 (the job list)          old [0, 109, 412, 714] -> new [0, 16, 412, 807]
node 133 (48px-wide right rail)  old [364, 109, 48, 714] -> new [0, 0, 0, 0]
node 126 (44x44 button)          old [251, 71, 44, 44]   -> new [331, 71, 44, 44]

The whole list moves up 93px and the element above it disappears. That is the desktop PageHeader (search + filter pills) being removed and the mobile bottom toolbar mounted: DiscoverJobsScreen branches on isMobile, and so does DashboardLayout (column gap, padding, the two-column Grid), the breadcrumb, and MarketingNav.

On the desktop preset the same build scores CLS 0.002. A PerformanceObserver probe in real Chromium at 1350px, with and without 4× CPU throttling, sees one 0.002 font shift and nothing else.

Why

packages/ui/src/hooks/useResponsive.ts:159 — the server snapshot is a constant:

const _SERVER_SNAPSHOT: Snapshot = { width: 1280, height: 900 }

So every SSR page is rendered as a desktop page for every visitor. On a phone the first client render (hydration, which React does against getServerSnapshot) matches — and then the store reads the real 390px, every responsive branch flips, and the page re-lays out. Anything below the first branch moves.

Two consequences, and the second is the bigger one:

  1. CLS ≈ 1 on every server-rendered page on every phone. .lighthouserc.json budgets it at 0.1. Phones are where search traffic lands.
  2. React regenerates the tree on every mobile load. When the flip lands inside the hydration pass (it does whenever a branch runs before useSyncExternalStore settles — the useHydrated/loader-data paths are fine, isMobile is not), the client tree differs in structure from the server's and React throws it away: "Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client." That is SSR: authenticated pages intermittently fail hydration and regenerate the tree — the session load races the first client render #681's symptom, and it explains why SSR: authenticated pages intermittently fail hydration and regenerate the tree — the session load races the first client render #681 "does not reproduce in isolation at any width" — the isolated probe measured at widths where nothing branches, and reproduces on any phone. Confirmed on the Public pages have no navigation, because the only drawer assumes a signed-in user #762 branch: at 800px (below the 1280 snapshot but above sm) /jobs/<slug> and /users/<slug> throw it on every load; at 1440 they never do.

What a fix looks like

The server has to render the layout the device will use. Sequence:

  1. @scaffald/ui: useResponsive reads its server snapshot from a context instead of the constant — ServerViewportProvider value={{ width, height }}, defaulting to today's 1280×900 so nothing changes for callers that do not set it. Scaffald/ui PR + pointer bump here.
  2. The app decides the snapshot per request. The route loader receives the Request; sec-ch-ua-mobile: ?1 or a UA sniff picks a phone snapshot (390×844) or the desktop one. The provider has to sit above the screens' hook calls, so it belongs in app/_layout.tsx (or the (public) group layout) and reads the loader's decision — whether a layout can read its child route's useLoaderData, or the decision travels another way (a +middleware header, a cookie), is the first thing to establish.
  3. Re-measure /jobs on the mobile preset; then set .lighthouserc.json budgets from measured reality with a ratchet (First Lighthouse baseline: /jobs has a 94s LCP, and no public page meets its budget #774's last acceptance box, deferred to here).

Not a fix: making the app's breakpoints CSS-only. That is the SCF parity work (#622) and far larger.

Acceptance

  • /jobs on Lighthouse's mobile preset: CLS < 0.1, and the page renders the mobile layout in the server HTML (no PageHeader search input in the markup for a phone UA)
  • /jobs/<slug> and /users/<slug> at 390px and at 800px: no "Hydration failed" in the console, three loads each
  • Desktop numbers unchanged (CLS 0.002 on the desktop preset today)
  • .lighthouserc.json budgets set from the new measurement with a ratchet, and the nightly reads green for a reason

Related: #774 (where this surfaced), #681 (the symptom, seen from the other end), #679, #762 (the header adds one more isMobile branch to every public page).

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrontendUXbugpriority: high

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions