useSyncExternalStore loops during hydration when getServerSnapshot differs from getSnapshot (flushPending exceeded 50 iterations)

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

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

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

调研方向

Start with packages/redact/src/dom/dispatcher.ts around the cited lines 331–348 and trace how hydration state affects useSyncExternalStore snapshots and scheduled updates. Compare the reproduction's Redact and React runs, then inspect nearby hydration tests or add a focused regression case. Done means hydration settles without the iteration error and reaches the client snapshot, matching React's two renders.

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

描述

🤖 Issue drafted with Claude Code. Reproduced and verified before posting.

Version: @tanstack/redact 0.2.1 (also 0.2.0). Baseline: react/react-dom 19.3.1.

Hydrating a component whose getServerSnapshot differs from getSnapshot never settles: Redact re-renders it 50 times, then hydrateRoot throws flushPending exceeded 50 iterations — suspected infinite update loop. (a bare Error in production). React renders it twice.

This is the "still hydrating?" idiom used by Headless UI 2.x (useServerHandoffComplete, behind Popover, Dialog, Transition…), so any page with a Headless UI component hits it.

Reproduction

npm i @tanstack/[email protected] react@19 react-dom@19 jsdom, then node repro.mjs redact / node repro.mjs react:

// repro.mjs
import { JSDOM } from "jsdom"
const { window } = new JSDOM("<!doctype html><div id=root></div>")
Object.assign(globalThis, { window, document: window.document, HTMLElement: window.HTMLElement, Node: window.Node })

const redact = process.argv[2] === "redact"
const R = await import(redact ? "@tanstack/redact" : "react")
const { renderToString } = await import(redact ? "@tanstack/redact/server" : "react-dom/server")
const { hydrateRoot } = await import(redact ? "@tanstack/redact/dom-client" : "react-dom/client")

let renders = 0
function App() {
  const isServer = R.useSyncExternalStore(() => () => {}, () => false, () => true)
  renders++
  return R.createElement("p", null, isServer ? "server" : "client")
}

const root = document.getElementById("root")
root.innerHTML = renderToString(R.createElement(App))
renders = 0
try {
  hydrateRoot(root, R.createElement(App))
} catch (error) {
  console.log("hydrateRoot threw:", error.message || "Error")
}
setTimeout(() => console.log({ renders, text: root.textContent }), 100)
Renders hydrateRoot throws Final text
React 19.3.1 2 no client
Redact 0.2.1 52 yes client

Same result in Chromium with a Vite build (redact() plugin, Redact on server and client).

Likely cause

In dispatcher.ts#L331-L348, the render reads getServerSnapshot() while root.h is set. The effect's check() compares it with getSnapshot(), finds a difference and calls scheduleUpdate. That update is flushed while the root is still marked as hydrating, so it reads the server snapshot again, and check() runs again (the inline subscribe changes every render). Logging inside the loop shows rendered=true client=false hydrating=true at every iteration.

React switches to getSnapshot() once the hydration render has committed, so its second render reads the client value and stops.

Impact

On a TanStack Start storefront using a Headless UI Popover, this adds ~150 ms of main-thread work per page load (4× CPU throttling) and reports an uncaught error, though the page ends up interactive.

主要语言
TypeScript
星标
537
派生
22
平均合并
10 小时 27 分钟
30 天内合并 PR
40

环境准备

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

从这里开始

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

TanStack/redact 的其他 Issue

查看 TanStack/redact 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

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