Abort superseded Server Component HMR requests on the client
In `next dev`, rapid edits can start overlapping Server Component HMR refreshes, but only the newest can commit. With this change, when a newer HMR refresh supersedes an older one, we abort the older request on the client so it stops transferring and decoding RSC data. This is the client half of the feature; later changes also cancel the superseded request's server-side work. Everything here is gated on the `serverComponentsHmrCancellation` flag. The router's `hmrRefresh` method now tracks the newest refresh generation in an `AbortController` and aborts the previous one before scheduling the new one, so the superseded request tears down as early as possible, threading the abort signal through the HMR action, `refreshDynamicData`, `navigateToKnownRoute`, `spawnDynamicRequests`, and `fetchMissingDynamicData` down to the `fetch` call. An aborted request becomes a new `NavigationTaskExitStatus.Canceled` rather than a failure, so `finishNavigationTask` leaves its cache nodes for the newer navigation to fulfill and does not retry or fall back to an MPA navigation. `hmrRefreshReducer` also bails out when its generation was already aborted before it ran, which happens when an HMR action waits behind a Server Action in the router queue. The subtle part is how an aborted request's partially received response is handled. A superseded refresh can already have committed part of its tree with a Suspense boundary still streaming. If we simply aborted the fetch, React's Flight client would reject the still-pending rows, and a committed boundary reading such a row would throw into an error boundary. Instead, for a cancellable refresh we decode the response through a wrapper stream that we close, rather than letting the aborted fetch error it, so React marks the unresolved rows as halted: they suspend during render instead of rejecting, and the superseded boundary keeps showing its fallback until the newer response commits. Closing the stream also avoids the unclosed-stream memory leak that browsers retain as a GC root, the same leak #89610 fixed for prefetch streams by closing them with `allowPartialStream` rather than leaving them open. The wrapper is created synchronously so the decode still starts where `createFromFetch` would, preserving the server-latency debug timing. A development test suite covers three cases: a superseded request is aborted while the newest commits without a hard reload, a partially committed render whose Suspense boundary is left streaming does not surface an error when superseded, and disabling the flag preserves the previous behavior where the superseded request runs to completion.
Showing
Please
register
or
sign in
to comment