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

Aberta
#183 0 comentários 1 reação 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
28/100
Tipo de issue
Bug
Clareza
Razoavelmente clara
Status de atividade
Ativa
Stack de tecnologia
node.js, react, typescript, vite
Domínio
frontend, tooling

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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.

Linguagem predominante
TypeScript
Estrelas
640
Forks
32
Merge médio
1h 15min
PRs com merge (30d)
7

Preparar o ambiente

Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de TanStack/redact

Todas as issues de TanStack/redact

Issues semelhantes

Mais issues de TypeScript

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.