Skip to content
Source
Sign inSign up
TanStack/redact

Flight chunks read with `use()` are thrown instead of read, and Next.js hydration replaces the server DOM #89

Openmamal72(GitHub profile, opens in a new tab) opened this issue 2 comments

Version: @tanstack/redact 0.2.1 via withRedact, Next.js 16.4.3 (App Router, Turbopack, production), react/react-dom 19.3.1, Chromium. Unchanged on main (20f6dd6).

What happens

A Server Component passes a promise to a Client Component, which reads it with use(). During hydration, Redact reports Hydration mismatch. and replaces the server-rendered element, even though the content matches. React 19.3.1 hydrates the same page with no errors and keeps the server node.

Repro

// app/flight-promise/page.jsx
import Reader from './reader'
export default function Page() {
  return <main><Reader value={Promise.resolve('Read from the server')} /></main>
}

// app/flight-promise/reader.jsx
'use client'
import { use } from 'react'
export default function Reader({ value }) {
  return <p id="read">{use(value)}</p>
}

Comparing the same page in examples/next-demo (next build && next start, with and without NEXT_TEST_REFERENCE=1):

React 19.3.1Redact 0.2.1
Server <p id="read"> keptyesno, replaced
pageerrornoneHydration mismatch.

Impact

In a larger App Router app, the same promises are also thrown as bare values ({status: "resolved_model", value: null} and {status: "blocked", …}) instead of being read. Client interactions in the affected tree stop working, including dialogs opened from search params, search and locale switching.

Where

src/react/thenable.ts subscribes only when status === undefined. React's Flight client gives its chunks a status of their own (resolved_model, blocked, …), and they only initialize or settle once something calls .then(). React's trackUsedThenable subscribes to any unsettled thenable and then checks again, so a resolved_model chunk is read in the same pass. Redact throws it instead.

Related: #82. Once Redact suspends on such a chunk during hydration without a <Suspense> directly above it, the mismatch comes from the path described there. The PR for this issue avoids suspending on chunks that are already resolved, as React does; #82 still applies to promises that are really pending.

  1. Thanks for the clear reproduction. The small example now looks fixed, but I'll keep this open for the larger app's Flight failures. Could you share a runnable case that still throws the chunks or breaks dialogs, search or locale switching on 0.2.3 or newer?

    AI review notes: Independent Next 16.4.3 checks against the published 0.2.3 package keep the server paragraph, report no hydration error and leave the counter interactive in Chromium, Firefox and WebKit. The matching published 0.2.1 control replaces the paragraph and reports the mismatch in all three. The maintained example also passes in development and production on 0.2.3. The hydration retry fixes in #95 are included in 0.2.3. The larger application was not reproduced. These results do not establish every custom thenable status or synchronous render detail, so the passing example is not enough to settle that part of the report. The useful coverage in #90 is still tracked separately.

  2. Thanks for looking into this. I retested the larger app on 0.2.6 (Next 16.4.3, production build, Chromium) without the patch from #90, and the Flight failures are gone: no chunks are thrown and there are no unhandled rejections. Dialogs opened from search params (by tapping and by loading a ?p= link directly), search and locale switching all work, signed out and signed in. So I no longer have a failing case for that part, and I'm fine with closing this.

    Two things still differ from React in the same app:

    • A <noscript> with an element inside (Google Tag Manager's snippet) makes every page report Hydration mismatch. and rebuild the document. That's #141, with a minimal repro.
    • Three pages that call redirect() while the shell renders report Hydration mismatch. before redirecting. The response is a 200 with <!--$!--><template data-dgst="NEXT_REDIRECT;…"> inside a <Suspense> placed directly in <html>. The same markup hydrates cleanly in a minimal app, so I'll open an issue once I can reproduce it outside our app.

Sign in to join this conversation.