Vercel Functions using Node.js can now detect when a request is cancelled and stop execution before completion. This is configurable on a per-path basis, and includes actions like navigating away, closing a tab, or hitting stop on an AI chat to terminate compute processing early.
This reduces unnecessary compute, token generation, and sending data the user would never see.
To enable cancellation, add "supportsCancellation": true to your vercel.json configuration. You can apply it to specific paths or all functions:
{
"functions" : {
"api/*" : {
"supportsCancellation" : true
}
}
}
Once enabled, you can listen for cancellation using Request.signal.aborted or the abort event:
export const GET = async ( req : Request ) => {
const abortController = new AbortController ( ) ;
req . signal . addEventListener ( "abort" , ( ) => {
console . log ( "request aborted" ) ;
abortController .…