Skip to content
Source
Sign inSign up
TanStack/redact

SVG presentation attributes (fontSize, stopColor, textAnchor…) reach the DOM in camelCase, so the browser ignores them #81

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

Version: @tanstack/redact 0.2.1, attributeName() unchanged on main (20f6dd6b). Baseline: react / react-dom 19.3.1.

On SVG elements, attributeName() in packages/redact/src/core/attributes.ts kebab-cases only the props that start with clip, fill or stroke, and keeps every other name as written. React maps the camelCase presentation attributes to their hyphenated names, so under Redact fontSize, fontWeight, stopColor, textAnchor, markerEnd, xlinkHref and the rest reach the DOM in camelCase, and the browser ignores them. The prefix rule also fails the other way: clipPathUnits, which SVG spells in camelCase, becomes clip-path-units.

The server and the client produce the same markup, so hydration reports nothing: the SVG just draws wrong. We hit it on a logo whose word is a <text fontSize={58} fontWeight={500}>. Under Redact it renders at the inherited font size, and its gradient stops (stopColor) turn black.

Reproduction

npm i @tanstack/redact@0.2.1 react@19.3.1 react-dom@19.3.1 jsdom, then node repro.mjs react and node repro.mjs redact:

// repro.mjs
import { JSDOM } from "jsdom"
const { window } = new JSDOM("<!doctype html><div id=root></div>")
Object.assign(globalThis, { window, document: window.document, HTMLElement: window.HTMLElement, Node: window.Node })

const redact = process.argv[2] === "redact"
const R = await import(redact ? "@tanstack/redact" : "react")
const { renderToString } = await import(redact ? "@tanstack/redact/server" : "react-dom/server")
const { createRoot } = await import(redact ? "@tanstack/redact/dom-client" : "react-dom/client")
const { flushSync } = await import(redact ? "@tanstack/redact/dom" : "react-dom")

const h = R.createElement
const App = () =>
  h("svg", { viewBox: "0 0 100 20" },
    h("linearGradient", { id: "g" }, h("stop", { offset: 0, stopColor: "#fff", stopOpacity: 0.5 })),
    h("clipPath", { id: "c", clipPathUnits: "objectBoundingBox" }),
    h("text", { x: 0, y: 16, fontSize: 16, fontWeight: 500, textAnchor: "start", dominantBaseline: "middle" }, "365"),
    h("use", { xlinkHref: "#g", markerEnd: "url(#m)", vectorEffect: "non-scaling-stroke" }))

console.log("SSR:   ", renderToString(h(App)))
const root = document.getElementById("root")
flushSync(() => createRoot(root).render(h(App)))
console.log("client:", root.innerHTML)

renderToString and createRoot give the same names, in each runtime:

PropReact 19.3.1Redact 0.2.1
stopColor, stopOpacitystop-color, stop-opacitystopColor, stopOpacity
fontSize, fontWeightfont-size, font-weightfontSize, fontWeight
textAnchor, dominantBaselinetext-anchor, dominant-baselinetextAnchor, dominantBaseline
markerEnd, vectorEffectmarker-end, vector-effectmarkerEnd, vectorEffect
xlinkHrefxlink:hrefxlinkHref
clipPathUnitsclipPathUnitsclip-path-units

Each runtime's markup loaded in Chromium, inside a body set to font-size: 12px:

ComputedReact 19.3.1Redact 0.2.1
<text> font size, weight16px, 50012px, 400
<stop> stop colorrgb(255, 255, 255)rgb(0, 0, 0)
clipPath.clipPathUnitsobjectBoundingBoxuserSpaceOnUse

Possible fix

React keeps an explicit alias map instead of a prefix rule (packages/react-dom-bindings/src/shared/getAttributeAlias.js): strokeWidth → stroke-width, fontSize → font-size, xlinkHref → xlink:href, and every name it doesn't list stays as written. Reusing that list in attributeName() would fix both directions. SSR, the client's setAttribute and hydration validation all go through that one function, so they would stay in sync.

Related: #42, about the same function lowercasing case-sensitive names (patternUnits).

  1. Confirmed on 0.2.1 with Next.js 16.4.3 (withRedact, production build, Chromium). The component below renders only on the client, after useEffect, so no server markup is involved:

    'use client'
    import { useEffect, useState } from 'react'
    import { QRCodeSVG } from 'qrcode.react'
    
    export default function SvgCheck() {
      const [mounted, setMounted] = useState(false)
      useEffect(() => setMounted(true), [])
      if (!mounted) return null
      return (
        <>
          <QRCodeSVG value="https://example.com" size={64} />
          <svg width="120" height="40">
            <text x="60" y="20" textAnchor="middle" paintOrder="stroke" strokeWidth="2">chart label</text>
          </svg>
        </>
      )
    }
    React 19.3.1Redact 0.2.1
    qrcode.react 4.2.0 <QRCodeSVG> pathsshape-rendering, computed crispedgesshapeRendering, computed auto
    <text textAnchor="middle">text-anchor, computed middletextAnchor, computed start
    <text paintOrder="stroke">paint-order, computed strokepaintOrder, computed normal
    strokeWidthstroke-widthstroke-width (correct)

    So every QRCodeSVG rendered on the client loses crispEdges, and centered SVG text such as chart labels moves to the start position. Together with #85, this keeps us from using Redact in production.

  2. can confirm. it did not convert stopColor to stop-color, causing my svg to go totally black

  3. Honestly, I'm disappointed with this library. I've even fallen out of love with NextJS because of its slowness. I'm looking at Astro + Preact with islands—it means less unnecessary JS code goes to the client, and everything works faster. I don't understand the point of this library if everything is so slow and clumsy.

    I tried my best and found bugs, did a PR on my fork, but the admins are just dragging their feet and not really making fixes. Therefore, I conclude that this library is useless because it's too much work, and the end result is 25KB of JS code. Install Astro and you'll be happy.

Sign in to join this conversation.