Skip to content

Bug: a dehydrated Suspense boundary misses a context change when a sibling has work in the same lanes, then hydrates with the new value (#418) #38398

Backlog

Description

@LouisCuvelier

React version: 19.3.1 (npm). Also reproduced with 19.3.0-canary-92bb0678-20261124 and with the copy vendored in Next.js 16.4.6 (19.3.0-canary-278794d7-20261002). The code involved is unchanged on main today.

Steps To Reproduce

  1. npm i react react-dom jsdom
  2. Save the script below as repro.cjs, then run NODE_ENV=production node repro.cjs.

A provider changes its value in a layout effect, right after the shell hydrates. Below it, a static element holds a dehydrated <Suspense> whose content reads the context, and a sibling that schedules its own update in the same commit.

Link to code example:

// npm i react react-dom jsdom && NODE_ENV=production node repro.cjs
const { JSDOM } = require('jsdom');
const dom = new JSDOM('<!doctype html><html><body><div id="root"></div></body></html>');
for (const key of ['window', 'document', 'navigator', 'Node', 'HTMLElement', 'Element', 'Event', 'HTMLIFrameElement']) {
  globalThis[key] = key === 'window' ? dom.window : dom.window[key];
}
const React = require('react');
const { hydrateRoot } = require('react-dom/client');
const { renderToString } = require('react-dom/server');
const { createContext, useContext, useLayoutEffect, useState, Suspense } = React;
const h = React.createElement;

const Route = createContext('a');

// Reads the context. Its markup differs between "a" and "b".
function Page() {
  return useContext(Route) === 'a' ? h('p', null, 'page a') : h('b', null, 'page b');
}

// A sibling of the boundary that schedules an update right after hydration.
function Badge() {
  const [count, setCount] = useState(0);
  useLayoutEffect(() => setCount(1), []);
  return h('span', null, count);
}

// A static element, so the context change has to propagate through a bailout.
const shell = h('header', null, h(Suspense, { fallback: null }, h(Page)), h(Badge));

function Router() {
  const [route, setRoute] = useState('a');
  useLayoutEffect(() => setRoute('b'), []);
  return h(Route.Provider, { value: route }, shell);
}

const root = document.getElementById('root');
root.innerHTML = renderToString(h(Router));
const errors = [];
hydrateRoot(root, h(Router), {
  onRecoverableError: (error) => errors.push(error.message.split('\n')[0]),
});
setTimeout(() => {
  console.log('DOM:', root.innerHTML);
  console.log('recoverable errors:', errors.length ? errors : 'none');
}, 500);

The current behavior

DOM: <header><b>page b</b><span>1</span></header>
recoverable errors: [
  'Minified React error #418; visit https://react.dev/errors/418?args[]=HTML&args[]= …'
]

In development, the error reads "Hydration failed because the server rendered HTML didn't match the client". React then client-renders the whole <header>.

Remove h(Badge) and the error goes away. The boundary hydrates with "a" first, and the update to "b" then applies:

DOM: <header><!--$--><!--/$--><b>page b</b></header>
recoverable errors: none

What we traced with an instrumented build:

  1. The render for the context change reaches <header>. Its childLanes already include the render lanes, because of Badge's update. So bailoutOnAlreadyFinishedWork clones the children without calling propagateParentContextChanges.
  2. The dehydrated <Suspense> then goes through attemptEarlyBailoutIfNoScheduledUpdate. The SuspenseComponent case returns null as soon as state.dehydrated !== null, also without propagating. The boundary never learns that the context changed, so it gets no childLanes and no retryLane bump.
  3. The update commits with the boundary still dehydrated. When React hydrates the boundary later, it reads the new value "b" against the HTML rendered for "a", and throws #114 - stop requiring docblock for JSX transformer #418.

The ActivityComponent case returns early the same way for a dehydrated Activity. We have not built a separate repro for it.

The expected behavior

The boundary hydrates with "a", then the update to "b" applies, with no hydration error. That is what happens without the busy sibling. It also matches what #22692 describes as the intent: a context change above a dehydrated boundary forces it to hydrate first, because React cannot know whether something inside reads the context.

A local patch that calls propagateParentContextChanges in that dehydrated branch, and goes to updateSuspenseComponent when the boundary then has work in the render lanes, removes the error in our app. We did not check it against React's test suite.

Where we hit it

A Next.js App Router app (cacheComponents) keeps a parallel-route slot, a breadcrumb, in a <Suspense> inside a layout. The same header holds client components that update right after hydration. Reload a page, then press Back within a second: the slot's dehydrated boundary misses the router's context change and hydrates the old page's HTML with the new page's tree. We worked around it by rendering that <Suspense> from a client component that reads the route, so the boundary gets a fresh element on every navigation.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions