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

Đang mở
#177 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
20/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
react, typescript
Lĩnh vực
frontend, testing

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
TypeScript
Star
581
Fork
28
Merge trung bình
13 giờ 48 phút
Pull request đã merge (30 ngày)
45

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của TanStack/redact

Tất cả issue của TanStack/redact

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.