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 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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分
マージ済み PR(30日)
7

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

TanStack/redact のほかの issue

TanStack/redact の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。