dillon1000/react

Commit

Add lazy reasons to browser() (#37241)

Changes `ReactDOM.browser()` to return a cheap branded recoverable token
instead of eagerly constructing an `Error`. It accepts an optional
reason string or initializer that runs only when a server renderer
consumes the token and may return any value; the client renderer ignores
the reason without invoking the initializer, so browser-only rendering
does not pay for an unused stack.

When Fizz consumes the token through `use()` or `abort()`, it creates a
consistent browser-bailout error at the consumption point so its stack
identifies the relevant operation. The initialized reason is preserved
unchanged as the optional `cause`, allowing strings, errors, and
structured framework metadata without runtime validation. If an
initializer throws, Fizz substitutes a stable diagnostic fallback so
reason generation cannot change rendering control flow. Successful
recoveries report the error through `onBrowserBailout`.

When no Suspense boundary can recover the render, Fizz clones the
branded recoverable error into an unbranded fatal diagnostic while
preserving its cause and consumption frames. During an abort, the
request retains the original branded error so every remaining task
observes the same reason; fatal clones are created only when reporting a
fatal root or closing the stream. Centralized recoverable logging uses
the brand to route successful bailouts through `onBrowserBailout` and
fatal clones through `onError`. The empty recoverable digest and client
hydration suppression behavior remain unchanged.

Tests cover omitted and direct reasons, lazy string, error, structured,
and primitive reasons, repeated use sites, throwing initializers, lazy
client behavior, consumption stacks, flattened fatal errors, recoverable
and fatal use and abort paths, nested aborts, direct throws, debug
tools, and development and production rendering.
Browse files
Changed paths8 files
First-parent comparison
M packages/react-debug-tools/src/ReactDebugHooks.js ModifiedM packages/react-dom/src/__tests__/ReactDOMBrowser-test.js ModifiedM packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js ModifiedM packages/react-dom/src/__tests__/ReactDOMServerSuspense-test.internal.js ModifiedM packages/react-dom/src/shared/ReactDOMBrowser.js ModifiedM packages/react-server/src/ReactFizzHooks.js ModifiedM packages/react-server/src/ReactFizzServer.js ModifiedM packages/shared/ReactTypes.js Modified
Patch

Files changed

Rendering syntax-highlighted changes…