`useId` values shift when a `Suspense` boundary suspends during hydration, so the server DOM is replaced (0.3.1)

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

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

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

调研方向

Start with the linked fix commit and compare its changes to server/dispatcher.ts and dom/dispatcher.ts, where the issue says the useId() counters advance. Read tests/use-id-hydration-order.test.tsx for the retry and lazy-loading cases. Done means those cases preserve server ids and DOM nodes, older markers still hydrate as described, and the suite passes.

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

描述

When a child of a resolved Suspense boundary suspends while the boundary hydrates, Redact skips the boundary and retries it once the data is there. The useId() values it hands out on the retry no longer match the server's, and neither do the ones of every component after the boundary. The attribute mismatch then makes Redact discard the server DOM and render it again on the client. With no boundary above, that is the whole page. React keeps every node.

This is the retry side of #155, which covers a lazy component that is loaded on the server but not yet in the browser. Both cases come from the same counter, and the fix below covers both.

We hit it on intlayer.org: our navbar sits in a boundary whose content reads a dictionary that is still loading on the first client render. The navbar dropdown got :R2 instead of the server's :R0, and the whole page was rendered again ~5 s after load. Chrome then reported the re-created <h1> as the LCP: 5.5 s instead of 0.35 s on a throttled phone.

Repro
import { Suspense, useId } from 'react'
import { renderToString } from 'react-dom/server'
import { hydrateRoot } from 'react-dom/client'

let isHydrating = false
let isLoaded = false
let resolveData
const data = new Promise((resolve) => {
  resolveData = () => {
    isLoaded = true
    resolve()
  }
})

const Label = ({ name }) => <p id={`${name}-${useId()}`}>{name}</p>

// Suspends on its first client render, like data still loading in the browser
const DataLabel = () => {
  if (isHydrating && !isLoaded) throw data
  return <Label name="data" />
}

const App = () => (
  <main>
    <h1>Title</h1>
    <Suspense fallback={<p>loading</p>}>
      <Label name="nav" />
      <DataLabel />
    </Suspense>
    <Label name="after" />
  </main>
)

const container = document.getElementById('root')
container.innerHTML = renderToString(<App />)
const title = container.querySelector('h1')

isHydrating = true
hydrateRoot(container, <App />, { onRecoverableError: console.error })
setTimeout(resolveData, 10)
// after the retry: container.querySelector('h1') === title ?
Server ids Ids after hydration <h1> kept Errors
React 19.3.1 _R_6_, _R_a_, _R_3_ same yes none
Redact 0.3.1 :R0, :R1, :R2 :r2, :r3, :r1 no Hydration mismatch.
Cause

Both sides number useId() with a counter in render order: frame.idCounter++ in server/dispatcher.ts, root.ic++ in dom/dispatcher.ts.

  1. The first hydration pass renders nav (:R0), then DataLabel suspends. The boundary is rewound and marked for a retry, but the counter isn't, so after takes :R1 instead of :R2.
  2. The retry continues from that counter, so nav and data no longer get :R0 and :R1 either.

React doesn't have this problem because its hydration ids come from the component's position in the tree (treeContextId), not from an order.

Fix

https://github.com/aymericzip/redact/commit/bb4c4be (branch fix/use-id-hydration-order)

  • Server: a resolved boundary's marker now also carries the range of ids its content consumed: <!--$<id>:<from>:<to>-->.
  • Client: hydration starts the boundary's ids at from, and continues after to, whether the boundary hydrates in order, after its lazy child loads, or on a retry.
  • Compatibility: markers without a range ($? streamed boundaries, older HTML) keep the current behavior.

It comes with tests/use-id-hydration-order.test.tsx, which covers both the retry case and the #155 case. Both tests fail on main and pass with the fix, and the full suite still passes. The one test that expected <!--$0--> now expects <!--$0:0: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 摘要。