`useId` values shift when a `Suspense` boundary suspends during hydration, so the server DOM is replaced (0.3.1)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 38/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- react, typescript
調査の方向性
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.
- The first hydration pass renders
nav(:R0), thenDataLabelsuspends. The boundary is rewound and marked for a retry, but the counter isn't, soaftertakes:R1instead of:R2. - The retry continues from that counter, so
navanddatano longer get:R0and:R1either.
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 afterto, whether the boundary hydrates in order, after itslazychild 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分
- マージ済み PR(30日)
- 7
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
TanStack/redact のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
TanStack/redact#195 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
TanStack/redact#142 · コメント 4 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
TanStack/redact の issue をすべて見る
似ている issue
-
bug data-types
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
ClickHouse/clickhouse-js#1293 ·
メンテナーはふだん 1 日以内に返信
-
area: middleware enhancement good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
agent/quality testing
難易度 1/5 1時間未満 初心者へのやさしさ 95/100
メンテナーはふだん 1 日以内に返信
-
GitHub registry install via `gh` credentials rewrites `\u0000`–`\u001f` escapes in source files to `^@`–`^_`対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 92/100
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
juspay/agent-distro#203 ·
メンテナーはふだん 1 日以内に返信