r/nextjs • u/Which-Examination-74 • 6h ago
With a slow client chunk, a non-transition state update made React discard server HTML under loading.tsx
On a client's storefront (next 16.3.0, react-dom 19.2.4) the product configurator page showed a blank spinner for about 1.4 s instead of the server-rendered page.
An e2e test caught it during development work. It runs against the production server and answers the one client chunk that holds the page root 1500 ms late, to widen the race. At the base commit it was red on both browser projects.
Same result on 3 of 3 loads. The useId-style ids had changed to client ids (our log says the tab ids became _r_0_) and the server-rendered <main> had been replaced. React threw the server HTML away and client-rendered the segment, with no console error.
The trigger was our analytics provider, a client component in the layout above the route segment. For a returning visitor with consent already stored, it called setConsent in an effect, outside any transition. That update reached the loading.tsx Suspense boundary while the boundary was still dehydrated.
We wrapped the provider's two mount-time updates in startTransition. Roughly, with a placeholder value, one of them now reads:
useEffect(() => {
startTransition(() => {
setConsent(value);
});
}, []);The test went green, 6/6.
Our notes say a transition that reaches a dehydrated boundary is held back, whereas a default-lane update is committed and discards the server HTML. Grant and deny on click stay urgent, because the banner has to close on click.
What it costs, measured after the fix: with the chunk 3000 ms late, the server-rendered cookie banner was still visible at 1500 ms, because it stays until hydration, and a tap in that window still discards the server HTML. After Reject, the opt-out happens one render later, 48 ms later than before the fix. No analytics event goes out without consent.
In a probe, next-themes discarded the server HTML the same way when the stored theme differed from the OS theme. We haven't fixed that, and it went into a follow-up task.