Support response aborting (#51594)
### What? This is an alternative to #51330, which only support aborting the response (doesn't support back-pressure). If the client cancels the request (HMR update, navigates to another page, etc), we'll be able to detect that and stop pulling data from the dev's `ReadableStream` response. ### Why? We want to allow API routes to stream data coming from another server (eg, AI services). The responses from these other servers can be long running and expensive. In the case the browser aborts the connection, it's critical that we stop streaming data as soon as possible. ### How? By checking whether `response.closed` is set during the `for await (…)` iteration, we're able to detect that the client has aborted the connection. Cleanup of the `ReadableStream` is handled implicitly by the async iterator when the loop ends. The one catch is our use of http-proxy for worker processes. It does not properly detect a client disconnecting (but does handle back-pressure). In order to fix that, I've manually added event listeners to detect the disconnect and cancel the proxied req/res pair. Re: [WEB-1185](https://linear.app/vercel/issue/WEB-1185) (we still need back-pressure) Fixes https://github.com/vercel/next.js/issues/50364 Fixes https://github.com/vercel-labs/ai/issues/90
Showing
Please
register
or
sign in
to comment