useSyncExternalStore loops during hydration when getServerSnapshot differs from getSnapshot (flushPending exceeded 50 iterations) #79
🤖 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/redact@0.2.1 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.
tannerlinsley closed this
Sign in to join this conversation.
Thanks for reporting this! This looks like it was fixed in Redact 0.2.2 by #95. Please try the latest version, and if you still see it, feel free to reopen this or share a fresh reproduction so we can take another look.