Skip to content

fix: render <style> so the colors are in the server HTML - #155

Merged
abernier merged 1 commit into
mainfrom
fix/ssr-style-tag
Jul 31, 2026
Merged

fix: render <style> so the colors are in the server HTML#155
abernier merged 1 commit into
mainfrom
fix/ssr-style-tag

Conversation

@abernier

Copy link
Copy Markdown
Owner

<Mcu> injects its <style> from useInsertionEffect. Effects only run in the browser, so on an SSR or SSG page the tag reaches the client empty: every --md-sys-color-* is undefined for the first paint, and anything reading one paints with no color at all until hydration fills it in.

It's live today — docs.pmnd.rs serves <style id="mcu-styles"></style> and 0 occurrences of md-sys-color across 6 MB of HTML:

curl -sL https://docs.pmnd.rs/react-three-fiber/getting-started/introduction \
  | grep -c 'md-sys-color'   # 0

It bites hardest on a static export, where the browser has the whole page and paints it long before any JS lands.

The change

McuProvider renders the tag instead:

-  useInsertionEffect(() => {
-    let tag = document.getElementById(styleId);
-
-    tag.textContent = css;
-  }, [css, styleId]);
-
-  return <Provider value={value}>{children}</Provider>;
+  return (
+    <Provider value={value}>
+      <style id={styleId} dangerouslySetInnerHTML={{ __html: css }} />
+      {children}
+    </Provider>
+  );

Mcu is already "use client", and Next renders client components on the server for the initial HTML — so the CSS ships in the markup, hydration matches (css is a pure function of the props), and setMcuConfig still works because React updates the tag's content on re-render.

Two things I deliberately didn't do:

  • <style href precedence>, React 19's hoist-to-<head> API. React treats hoisted stylesheets as immutable and keyed by href, so a theme change would either be dropped or leak a new sheet per change. Wrong fit for a provider whose whole job is changing the sheet.
  • Keeping the tag in document.head. A portal into head doesn't server-render, which is the bug. So the tag now sits where <Mcu> is — same id, same cascade, valid everywhere. That's the one observable behaviour change, hence a minor.

Tests

Mcu.test.tsx gains a renderToStaticMarkup case asserting the CSS is in the server markup — both the :root and .dark halves — and that it isn't HTML-escaped (which is why it's dangerouslySetInnerHTML and not children: React escapes text children, and a & or > in a selector would come out as an entity).

Verified it fails on main and passes here. The two existing tests are untouched and still pass; they query document.querySelector("#mcu-styles"), which doesn't care where the tag lives.

pnpm run lgtm clean.

Follow-up

This fixes the FOUC for <Mcu> users. Separately, builder still can't be called from a Server Component — dist/index.js carries a hoisted "use client" banner, so an RSC gets Attempted to call builder() from the server but builder is on the client, and generating the CSS at build time means shelling out to the CLI. Happy to send that as its own PR.

🤖 Generated with Claude Code

`useInsertionEffect` only runs in the browser, so an SSR/SSG page shipped
the tag empty: every `--md-sys-color-*` was undefined for the first paint,
and anything reading one painted with no color at all until hydration.
docs.pmnd.rs still serves `<style id="mcu-styles"></style>` for this reason.

Rendering the tag puts the CSS in the HTML the server sends, and React
updates its content in place on `setMcuConfig`. Not `<style href
precedence>` -- React treats hoisted stylesheets as immutable and keyed by
`href`, so a theme change would be dropped or leak a sheet per change.

The tag now sits where `<Mcu>` is rather than in `document.head`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@abernier
abernier merged commit c7394fe into main Jul 31, 2026
6 checks passed
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