Skip to content
Dashboard

Node.js Vercel Functions now support request cancellation

Craig Andrews,

Request cancellation is now supported at the route level; you have to manually enable it in vercel.json. Read more .

Vercel Functions using Node.js can now detect when a request is cancelled and stop execution before completion. This 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 never see.

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.abort();
});
const res = await fetch("https://my-backend-service.example.com", {
headers: {
Authorization: `Bearer ${process.env.AUTH_TOKEN}`,
},
signal: abortController.signal,
});
return new Response(res.body, {
status: res.status,
headers: res.headers,
});
};

If you're using the AI SDK, forward the abortSignal to your stream:

import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
export async function POST(req: Request) {
const { prompt } = await req.json();
const result = streamText({
model: openai('gpt-4-turbo'),
prompt,
// forward the abort signal
abortSignal: req.signal,
});
return result.toTextStreamResponse();
}

Learn more about cancelling Function requests.

Reference Architecture

Choose a stack and work through the decisions behind it, with curated documentation, guides, templates, and courses to help you build.

  • Agent stack: Build and operate agents with models, tools, and durable workflows.
  • Web applications: Choose rendering, data, authentication, and caching approaches.
  • Backends: Choose a runtime, connect services, and run background work.

Browse all stacks

Build with prompts

Build an app, extend an agent, or improve your project with ready-to-use prompts for your coding agent.