Skip to content
Source
Sign inSign up
TanStack/redact

camelCase SVG attributes (patternUnits, ...) are lowercased, causing silent hydration re-render and broken patterns #42

Send to my agent
Opentuboihirokidesu(GitHub profile, opens in a new tab) opened this issue 3 comments

Env: @tanstack/redact@0.1.4, TanStack Start (prerendered), Chromium 154. Same code on main (packages/redact/src/core/attributes.ts).

camelCase SVG attributes are lowercased, so hydration silently re-renders the boundary

Where: attributeName() in dist/core/attributes.js (L7–19). For SVG it only kebab-cases names matching /^(?:clip|fill|stroke)/, special-cases viewBox, and falls through to name.toLowerCase() for everything else. Case-sensitive SVG attributes such as patternUnits, patternTransform, gradientUnits, gradientTransform, preserveAspectRatio, stdDeviation, markerWidth, refX etc. are therefore emitted as patternunits, patterntransform, ...

This breaks two things:

  1. Hydration mismatch. The browser's HTML parser fixes the case of known SVG attributes inside <svg> (patternunits → patternUnits), but validateHydrationProps looks up getAttribute("patternunits"), which is case-sensitive on SVG elements and returns null. Hydration aborts and the enclosing Suspense boundary is client-rendered. Under TanStack Start that boundary is the route's <main>, so the whole page body is thrown away and rebuilt on every load, with nothing logged unless onRecoverableError is passed.
  2. Wrong rendering on the client. The rebuilt element gets the lowercase attribute via setAttribute, which SVG does not recognise. pattern.patternUnits.baseVal is 2 (objectBoundingBox) instead of 1 (userSpaceOnUse), so the pattern does not render. React renders it correctly.

Side effects we measured in a real app: loading="lazy" images inside the rebuilt subtree load eagerly, the LCP element is replaced after hydration (Lighthouse observed LCP moves from FCP to after hydration), and the re-render adds to the long hydration task.

Repro (jsdom/vitest, also reproduced in Chromium):

import { createElement as h } from '@tanstack/redact';
import { renderToString } from '@tanstack/redact/server';
import { hydrateRoot } from '@tanstack/redact/dom-client';
import { flushSync } from '@tanstack/redact/dom';

const App = () =>
  h('div', null,
    h('svg', null,
      h('defs', null,
        h('pattern', { id: 'p', width: '10', height: '10',
          patternUnits: 'userSpaceOnUse', patternTransform: 'rotate(45)' })),
      h('rect', { fill: 'url(#p)' })));

const html = renderToString(h(App));
const c = document.createElement('div');
c.innerHTML = html;
document.body.append(c);
const before = c.firstChild;
flushSync(() => {
  hydrateRoot(c, h(App), { onRecoverableError: (e) => console.warn(String(e)) });
});
console.log(html, c.firstChild === before);
<div><svg><defs><pattern id="p" width="10" height="10" patternunits="userSpaceOnUse" patterntransform="rotate(45)"></pattern></defs><rect fill="url(#p)"></rect></svg></div>
Error: Hydration attribute mismatch on <pattern> for "patternunits": expected "userSpaceOnUse" but found nothing.
false

With react-dom the same component serializes patternUnits="userSpaceOnUse", hydrates without errors and keeps the server nodes.

Workaround: render the <pattern> markup through dangerouslySetInnerHTML on <defs> so the SVG parser handles the casing.

Suggested fix: in the SVG namespace, keep the prop name as written and only map a known list to kebab-case (strokeWidth → stroke-width, fontSize → font-size, ...), which is how react-dom behaves. Lowercasing should only apply to HTML elements. Sharing the same mapping between SSR, client setAttribute and validateHydrationProps would keep the three in sync.

  1. Hit this too, on @tanstack/redact@0.1.5 with TanStack Start on Cloudflare Workers. I checked the 0.2.0 source and attributeName() in src/core/attributes.ts still has the same logic.

    In our case the trigger is a latency chart: preserveAspectRatio="none" on the <svg> and gradientUnits on a <linearGradient>. Hydration fails on the first one, and the route's whole <main> is rebuilt. All 177 server nodes on the page get replaced. Our Playwright tests caught it as flaky "element is not attached to the DOM" errors.

    One more case that isn't in the report above. Presentation attributes outside clip*, fill* and stroke* get lowercased, not kebab-cased. So stopColor and stopOpacity come out as stopcolor and stopopacity, which browsers ignore. The gradient stops lose their color even when hydration isn't involved. This is from our SSR output:

    <stop offset="0" stopcolor="var(--muted-foreground)" stopopacity="var(--chart-fill-top)">

    React renders stop-color and stop-opacity here. textAnchor, dominantBaseline and fontSize go through the same branch, so I'd expect them to break the same way. I haven't tested those three.

    The fix suggested above would cover our case: keep SVG names as written, kebab-case a known list of presentation attributes, and use one mapping for SSR, setAttribute and hydration validation.

  2. Still reproduces on 0.2.0, and it also hits preserveAspectRatio and gradientUnits.

    Minimal check in jsdom (renderToString, then hydrateRoot with onRecoverableError):

    const App = () =>
      h(Suspense, { fallback: null },
        h('main', null,
          h('svg', { viewBox: '0 0 10 10', preserveAspectRatio: 'none' },
            h('linearGradient', { id: 'g', gradientUnits: 'userSpaceOnUse' }))))
    server <main> keptonRecoverableErrorattribute after hydration
    react 19.3.1yesnonepreserveAspectRatio
    redact 0.2.0noHydration mismatch.preserveaspectratio

    One knock-on effect that took us a while to trace: once the boundary is client-rendered, a lazy() child inside it that hasn't loaded yet renders its fallback instead of keeping the server HTML. In our TanStack Start app the lower sections of the landing page are in such a part, so a ref callback on their parent that sets up an IntersectionObserver finds no sections to observe. With React the same callback observes all three. Scroll-linked state then silently never updates, and nothing is logged, since Start doesn't pass onRecoverableError.

  3. Possible fix for stopColor / stopOpacity: https://github.com/ilbertt/nibrun/pull/1021. Adds stop to the SVG attribute prefix mapping.

Sign in to join this conversation.