Repository navigation
fix(providers): keep server HTML of lazy CodeBlocks and charts on mount (3.0) - #364
Conversation
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
…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
|
Visual check (Compare): one shot changed: 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 610f70b makes the recharts loader wait for Generated by Claude Code |
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/docsfrom 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.lazybehind<Suspense fallback={null}>. As the providers mount, they set state:setThemeSynced(true)always runs, andresolvedThemechanges whenever ThemeInit painted something other than the server default. The[theme]effect also always created a new style object.setMounted(true).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
nullfallback. A React DevTools-hook trace showed the collapse commit was the one where ThemeProvider and DataThemeProvider re-rendered.Change
startTransition, so boundaries that are still hydrating keep their server HTML.[theme]effect bails out when the theme didn't change.themeValue,styleValueand the data-theme value are memoized.handleSetChartOptionsis auseCallback.camelToKebabmoved to module scope.Checks
data-themeand the stored value.provider-hydration-suspense.test.tsx. It hydrates all three providers around a pendingReact.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 andactdon'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 testin packages/core: 3,892 passed.tscis clean. Biome error count is unchanged (4, all from before this change).Generated by Claude Code