Skip to content

fix(providers): keep server HTML of lazy CodeBlocks and charts on mount (3.0) - #364

Merged
lorant-one merged 3 commits into
feat/3-0-breaking-cleanupfrom
fix/codeblock-hydration-layout-shift
Oct 29, 2026
Merged

lorant-one merged 3 commits into
feat/3-0-breaking-cleanupfrom
fix/codeblock-hydration-layout-shift

Conversation

@lorant-one

Copy link
Copy Markdown
Member

Requested by Lorant · project thread

Why

In a Lighthouse audit of the Once UI sites, every docs component page scored CLS 0.28 on mobile and 0.32 on desktop (docs.once-ui.com/components/button). The code examples disappear after hydration and then come back.

I reproduced it on a local production build of apps/docs from this branch. The article is 8,317 px tall after streaming. At about 2.3 s, React removes all 12 CodeBlocks' server DOM at once, so the article drops to 3,573 px. It grows back to 8,317 px when the CodeBlock chunk arrives.

Cause

CodeBlock (and the charts, Map and MediaUpload) are React.lazy behind <Suspense fallback={null}>. As the providers mount, they set state:

  • ThemeProvider: in a layout effect, setThemeSynced(true) always runs, and resolvedTheme changes whenever ThemeInit painted something other than the server default. The [theme] effect also always created a new style object.
  • DataThemeProvider: setMounted(true).
  • LayoutProvider: the width goes from 0 to the window width.

The context values were also new objects on every render. So an urgent context change reached each Suspense boundary that was still waiting for its chunk. React could not hydrate those boundaries, so it threw away their server HTML and rendered the null fallback. A React DevTools-hook trace showed the collapse commit was the one where ThemeProvider and DataThemeProvider re-rendered.

Change

  • The mount-time updates in all three providers run inside startTransition, so boundaries that are still hydrating keep their server HTML.
  • The [theme] effect bails out when the theme didn't change.
  • themeValue, styleValue and the data-theme value are memoized. handleSetChartOptions is a useCallback. camelToKebab moved to module scope.

Checks

  • On the local docs build, the page now only grows while streaming (3,573 → 8,317 px) and never collapses, over 3 runs with a 6× CPU slowdown. Before the change it collapsed in every run.
  • Theme switching still works: dark, light and system all update data-theme and the stored value.
  • New test provider-hydration-suspense.test.tsx. It hydrates all three providers around a pending React.lazy, with ThemeInit having painted a different theme than the server rendered, and checks that the server node survives. It does not fail on the old code in jsdom, because jsdom and act don't reproduce the browser's timing. It did catch an intermediate version of this fix (transitions without memoized values) that dropped the server node, so it guards that path.
  • pnpm test in packages/core: 3,892 passed. tsc is clean. Biome error count is unchanged (4, all from before this change).

Generated by Claude Code

ThemeProvider, DataThemeProvider and LayoutProvider set state urgently as
they mounted, and their context values were new objects on every render.
The change reached every Suspense boundary still hydrating, so React
dropped the server HTML of lazy CodeBlocks and charts for the null
fallback until their chunk loaded: docs component pages collapsed by
~4,700 px and grew back (Lighthouse CLS 0.28 on mobile).

Mount-time updates are transitions, the [theme] effect bails out when
nothing changed, and the context values are memoized.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016gNJtxgzMmxuD2qeRfRW1z
@lorant-one lorant-one self-assigned this Oct 29, 2026
…y every load

Recharts measures each tick label once and caches its width. A label
measured before Geist loaded kept the fallback width, so the last label
of a chart sat flush with the edge on some loads and ~6 px in on others
(the docs' /data/setup LineChart). The visual check caught it once this
branch changed when charts first render. The recharts loader now waits
for document.fonts.ready.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016gNJtxgzMmxuD2qeRfRW1z

Copy link
Copy Markdown
Member Author

Visual check (Compare): one shot changed: data_setup@1280__a516556808, the three-segment LineChart on /data/setup, 149 px.

I looked into it, and it isn't a layout change from this PR. Recharts measures each tick label once and caches the width. The last label of each segment is placed flush with the edge using that cached width. Whether Geist had loaded at measurement time decided where that label landed. On the base build the 1900 label's x came out as 197.2 on one load and 191.4 on the next, with the same code (logged inside the capture script at shot time). 197.2 is the correct one: the text is 25.6 px wide and the segment is 210 px. 191.4 comes from a stale measurement. This PR made charts render earlier, so the race fell the other way on CI.

610f70b makes the recharts loader wait for document.fonts.ready before the first chart renders. Locally, four runs of capture.mjs on this head gave the same pixels for that shot each time, with the label at 197.2. Base still varies between loads, so this shot can differ from base. I'm adding visual-change for that one correction.


Generated by Claude Code

@lorant-one
lorant-one merged commit a10c37c into feat/3-0-breaking-cleanup Oct 29, 2026
8 checks passed
@lorant-one
lorant-one deleted the fix/codeblock-hydration-layout-shift branch October 31, 2026 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants