Fix: Unnecessary Suspense fallback triggered by URL update (#99612)
Every router state update is supposed to be wrapped in startTransition,
but restores weren't. A restore syncs the router with a userland
`history.pushState` / `history.replaceState`, a back/forward navigation
between those entries, or a page restored from the bfcache. The callers
dispatch it inside `startTransition`, but the action queue applied every
reducer result in a later microtask, after that scope had closed. So
anything that suspended on the new URL replaced the current content with
the nearest Suspense fallback.
The microtask came from wrapping the reducer in an async function, back
when reducers switched from throwing promises to returning them
(#56497). The queue already handles both kinds of results, so it now
calls the reducer directly and applies a synchronous result right away.
A reducer that throws is handled the same way a rejection was. This was
tried before in #77582 and reverted in #77909 because of a performance
regression that was seen internally.
Back/forward now gets React's usual popstate behavior: a Transition
that's scheduled during a `popstate` event renders synchronously.
This also removes the special case that dispatched the bfcache restore
as an urgent update. It was meant to stop the router from rendering a
stale MPA navigation and firing it again. But that state only exists in
a Transition that suspends and never commits, so a restore that's also a
Transition renders in the same pass. The bfcache test now covers this in
headless browsers too, by simulating the `pageshow` event.
Thanks to @controversial, who found the cause and wrote the original
test in #99537.
Co-authored-by:
Luke Deen Taylor <10377391+controversial@users.noreply.github.com>
Showing
Please
register
or
sign in
to comment