fix: render <style> so the colors are in the server HTML - #155
Merged
Conversation
`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>
This was referenced Jul 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
<Mcu>injects its<style>fromuseInsertionEffect. 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.rsserves<style id="mcu-styles"></style>and 0 occurrences ofmd-sys-coloracross 6 MB of HTML:It bites hardest on a static export, where the browser has the whole page and paints it long before any JS lands.
The change
McuProviderrenders the tag instead:Mcuis already"use client", and Next renders client components on the server for the initial HTML — so the CSS ships in the markup, hydration matches (cssis a pure function of the props), andsetMcuConfigstill 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 byhref, 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.document.head. A portal intoheaddoesn't server-render, which is the bug. So the tag now sits where<Mcu>is — sameid, same cascade, valid everywhere. That's the one observable behaviour change, hence aminor.Tests
Mcu.test.tsxgains arenderToStaticMarkupcase asserting the CSS is in the server markup — both the:rootand.darkhalves — and that it isn't HTML-escaped (which is why it'sdangerouslySetInnerHTMLand not children: React escapes text children, and a&or>in a selector would come out as an entity).Verified it fails on
mainand passes here. The two existing tests are untouched and still pass; they querydocument.querySelector("#mcu-styles"), which doesn't care where the tag lives.pnpm run lgtmclean.Follow-up
This fixes the FOUC for
<Mcu>users. Separately,builderstill can't be called from a Server Component —dist/index.jscarries a hoisted"use client"banner, so an RSC getsAttempted 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