1 min read
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.