|
| 1 | +/* Minimal reproduction of ahrefs/WEB-844: hydration mismatch (React error |
| 2 | + #418) caused by inline-style CSS custom properties serializing in a |
| 3 | + different order on the server (server-reason-react) than on the client |
| 4 | + (Melange/reason-react). |
| 5 | +
|
| 6 | + styled-ppx in CSS-extraction mode hoists every $(...) interpolation into an |
| 7 | + inline-style custom property and builds the style prop by folding |
| 8 | + ReactDOM.Style.unsafeAddProp over the vars, in declaration order (see |
| 9 | + styled-ppx packages/runtime/{native,melange}/CSS.ml). [makeStyle] below is |
| 10 | + that exact runtime, inlined. |
| 11 | +
|
| 12 | + reason-react's unsafeAddProp is Object.assign: a new key lands last, so the |
| 13 | + client keeps declaration order. server-reason-react <= 20260616 snapshots |
| 14 | + prepended in unsafeAddProp, so SSR emitted the vars reversed and React |
| 15 | + reported a hydration mismatch on every element carrying two or more hoisted |
| 16 | + vars. Fixed by c9daf826 ("Emit style properties in signature order, |
| 17 | + matching React", first released in 0.5.0). */ |
| 18 | + |
| 19 | +let makeStyle = vars => |
| 20 | + List.fold_left( |
| 21 | + (style, (key, value)) => |
| 22 | + ReactDOM.Style.unsafeAddProp(style, key, value), |
| 23 | + ReactDOM.Style.make(), |
| 24 | + vars, |
| 25 | + ); |
| 26 | + |
| 27 | +/* Mirrors the extracted CSS of the failing ahrefs.com `Link_Css.link`: a |
| 28 | + top-level `color: $(color)` plus a nested |
| 29 | + `@media (hover: hover) { &:hover { color: $(hoverColor) } }`. */ |
| 30 | +let extractedCss = {| |
| 31 | + .repro-link { cursor: pointer; color: var(--color-czybvw); } |
| 32 | + @media (hover: hover) { |
| 33 | + .repro-link:hover { color: var(--hoverColor-1eveqlc); } |
| 34 | + } |
| 35 | +|}; |
| 36 | + |
| 37 | +[@react.component] |
| 38 | +let make = () => { |
| 39 | + let style = |
| 40 | + makeStyle([ |
| 41 | + ("--color-czybvw", "#3A57FC"), |
| 42 | + ("--hoverColor-1eveqlc", "#F75A03"), |
| 43 | + ]); |
| 44 | + <DemoLayout background=Theme.Color.Gray2> |
| 45 | + <style> {React.string(extractedCss)} </style> |
| 46 | + <Stack gap=8 justify=`start> |
| 47 | + <h1 className="text-xl font-bold"> |
| 48 | + {React.string("Hydration: inline-style custom property order")} |
| 49 | + </h1> |
| 50 | + <p className="text-sm text-gray-500"> |
| 51 | + {React.string( |
| 52 | + "This anchor carries two hoisted custom properties, built exactly " |
| 53 | + ++ "like styled-ppx's CSS-extraction runtime does (a fold over " |
| 54 | + ++ "ReactDOM.Style.unsafeAddProp). If the server serializes them " |
| 55 | + ++ "in a different order than the client, hydration fails with " |
| 56 | + ++ "React error #418. Open the console: it must be free of " |
| 57 | + ++ "hydration errors, and hovering the link must turn it orange.", |
| 58 | + )} |
| 59 | + </p> |
| 60 | + <a className="repro-link text-l font-bold" href="#" style> |
| 61 | + {React.string("I must be blue, orange on hover, and hydrate clean")} |
| 62 | + </a> |
| 63 | + /* Adjacent text nodes under a static-optimizable element: SSR must |
| 64 | + emit a <!-- --> separator between them or hydration fails with |
| 65 | + React #418 (the ahrefs.com LandingHero H1 shape, WEB-844). */ |
| 66 | + <p className="text-sm"> |
| 67 | + {React.string("Adjacent")} |
| 68 | + {React.string(" text nodes must hydrate clean too ")} |
| 69 | + <i> {React.string("(text separator regression)")} </i> |
| 70 | + </p> |
| 71 | + </Stack> |
| 72 | + </DemoLayout>; |
| 73 | +}; |
0 commit comments