Vite plugin with environments: ['client'] hydrates React's server HTML with Redact-format useId values, so every useId attribute mismatches (0.3.1)

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

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
28/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
node.js, react, typescript, vite
领域
frontend, tooling

调研方向

Start with the Vite plugin's environment handling and the identifierStrategy option it passes, comparing it with the Next.js integration in src/next/client.ts. Reproduce the mismatch with the jsdom snippet in the issue, checking that useId output differs between _R_0_ and :r0. Done when hydration with environments: ['client'] produces React-compatible ids and reports no recoverable errors.

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

描述

Summary

With the Vite plugin set to replace the browser only, redact({ environments: ['client'] }), the server renders with React and Redact hydrates React's HTML. React's useId writes ids like _R_0_. Redact's client generates :r0 and doesn't match them, so every element whose attribute carries a useId value is a hydration mismatch, and the server's DOM is replaced.

In a TanStack Start app built this way, every server-rendered page reported Hydration mismatch. through onRecoverableError on load, because headless UI libraries (Base UI here) put useId values in id, htmlFor and aria-* attributes.

Redact already has the fix: identifierStrategy: 'react' makes the client produce React's ids, and the Next.js integration passes it (src/next/client.ts). The Vite plugin doesn't pass it when the SSR environment is left on native React, and there is no public way to opt in from a Vite app, since TanStack Start calls hydrateRoot itself.

Reproduction
// Server: real React
import { renderToString } from 'react-dom/server'
// Client: Redact
import { hydrateRoot } from '@tanstack/redact/dom-client'

function Field() {
  const id = useId() // from each runtime's own 'react'
  return (
    <div>
      <label htmlFor={id}>Email</label>
      <input id={id} />
    </div>
  )
}

container.innerHTML = renderToString(<Field />) // React
const serverInput = container.querySelector('input')
hydrateRoot(container, <Field />, { onRecoverableError: (e) => errors.push(e.message) }) // Redact

Run in jsdom with Node 26, each side using its own runtime's createElement and useId:

Production Development
Server HTML (React) <label for="_R_0_"> <input id="_R_0_"> same
After Redact hydrates <label for=":r0"> <input id=":r0"> same
Recoverable errors Hydration mismatch. Hydration mismatch on <label> for "htmlFor".
Server <input> kept no no

Passing identifierStrategy: 'react' to hydrateRoot gives _R_0_ on both sides, no errors, and keeps the server's <input>.

Expected

When the plugin leaves the server on native React (environments: ['client']), the client hydrates with React-compatible ids, as the Next.js and Vinext integrations do. Alternatively, the plugin could expose an option for it.

Related: #155 covers the same id scheme when Redact renders both sides, where ids shift with lazy timing.

Environment

@tanstack/redact 0.3.1, React 19.3.1, @tanstack/react-start 1.169.7, Vite+ 1.7.0 (Vite 8.3), Node 26.13.1.

主要语言
TypeScript
星标
640
派生
32
平均合并
1 小时 15 分钟
30 天内合并 PR
7

环境准备

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

从这里开始

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

TanStack/redact 的其他 Issue

查看 TanStack/redact 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

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