SSR adds px to numeric CSS custom properties, causing a hydration mismatch

未关闭
#177 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
20/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
react, typescript
领域
frontend, testing

调研方向

Read packages/redact/src/server/escape.ts and the linked test tests/ssr-custom-property-styles.test.tsx; compare how styleToString handles numeric values with how browser rendering treats keys beginning with --. The issue also proposes defaulting root.re in dom/root-internal.ts to reportRecoverableError. Tests already pass with the proposed changes on the linked branch, so check whether that work has been adopted before picking this up.

由索引模型根据 Issue 内容生成。

描述

Bug

During SSR, numeric CSS custom properties get a px unit:

renderToString(<span style={{ '--h': 262 } as React.CSSProperties} />)
// Redact: <span style="--h:262px;"></span>
// React:  <span style="--h:262"></span>

The browser renderer leaves custom properties unitless (--h: 262), so hydrateRoot hits Hydration mismatch on <span> for "style" and client-renders the whole root again.

Version: @tanstack/redact 0.3.1 (also on main at f61f721).

Seen in: a TanStack Start app with product logos styled style={{ '--h': hue }} and color: hsl(var(--h) 45% 50%).

  • 516 of 539 elements on / were recreated after load, and LCP went from ~52 ms to ~108 ms.
  • Until hydration finished, the logos had no colour, because hsl(262px …) is invalid.

Passing the value as a string (String(hue)) avoids it.

Cause: styleToString in packages/redact/src/server/escape.ts applies the numeric px rule to keys starting with -- as well. setStyleProperty in dom/[dom.ts](https://github.com/home/fak3/developer/dom/dom.ts) and React both skip custom properties.

Related: mismatch fallback is silent without onRecoverableError

With no onRecoverableError passed to hydrateRoot, this mismatch produced no console output at all in production; the page just re-rendered. React's default reports these through reportError. Redact already does the same for uncaught render errors (dom/[reconcile.ts](https://github.com/home/fak3/developer/dom/reconcile.ts)), so defaulting root.re to it would make this kind of bug visible. I found this one only by patching the bundle.

Fix

Both changes, with tests and a changeset, are on a branch you're welcome to take:
https://github.com/fakelozic/redact/commit/1ef30f4ed182d0a99f3a62ce01547893f415556c

  • server/escape.ts: keys starting with -- keep their value as written. One-line change.
  • dom/[root-internal.ts](https://github.com/home/fak3/developer/dom/root-internal.ts): re: options.onRecoverableError ?? reportRecoverableError, which uses reportError and falls back to console.error.
  • tests/[ssr-custom-property-styles.test.tsx](https://github.com/home/fak3/developer/tests/ssr-custom-property-styles.test.tsx): the new tests fail on main and pass with the fix.

pnpm test (2131 passed), pnpm test:types and pnpm size:check (Node 24.22.0) all pass. The repo only accepts pull requests from collaborators, so I couldn't open a PR.

主要语言
TypeScript
星标
581
派生
28
平均合并
13 小时 48 分钟
30 天内合并 PR
45

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/redact 的其他 Issue

查看 TanStack/redact 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。