How to propogate abort signal through api route?

I have an api route that calls another api like (openai completions, another nuxt api route or an agent). How can I make sure that when I abort the outer api, the inner api calls are aborted as well? I'm trying to do it with event.node.req.on("close") since "aborted" is deprecated according to https://nodejs.org/api/http.html Can't find any documentation on this and I would guess its a pretty standard usecase See the example below
// /server/api/routeA.get.ts
export default defineEventHandler(async (event) => {
// Create an AbortController tied to the client request
const controller = new AbortController()

// If the client disconnects, abort the controller
event.node.req.on('close', () => {
controller.abort()
})

try {
// Pass the abort signal to the inner fetch
const data = await $fetch('/api/routeB', {
signal: controller.signal
})

return {
message: 'Fetched routeB',
data
}
} catch (err) {
if (err.name === 'AbortError') {
return { error: 'Request aborted' }
}
throw err
}
})
// /server/api/routeA.get.ts
export default defineEventHandler(async (event) => {
// Create an AbortController tied to the client request
const controller = new AbortController()

// If the client disconnects, abort the controller
event.node.req.on('close', () => {
controller.abort()
})

try {
// Pass the abort signal to the inner fetch
const data = await $fetch('/api/routeB', {
signal: controller.signal
})

return {
message: 'Fetched routeB',
data
}
} catch (err) {
if (err.name === 'AbortError') {
return { error: 'Request aborted' }
}
throw err
}
})
2 Replies
kapa.ai
kapa.ai2mo ago
Hi @AxelSorensen. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Your use case—propagating an abort signal from an outer API route to inner API calls—is indeed common, but the official Nuxt documentation does not provide a direct, end-to-end example for this pattern. However, there are some relevant details in the knowledge sources: - Nuxt server API handlers use h3, and you can access the underlying Node.js request via event.node.req. - The documentation shows that you can use event.waitUntil for background tasks, but it does not mention aborting fetches or propagating abort signals through API routes. - There is no explicit mention in the docs about using AbortController or passing signal to $fetch in server routes, nor about handling event.node.req.on('close') for aborting downstream requests. - The recent PRs in the Nuxt repo discuss adding AbortSignal support to useAsyncData, but this is for client/server data fetching, not for server API route-to-route communication. What is documented: - When making fetch requests in server routes, you can use event.$fetch to forward the request context and headers, but there is no mention of abort signal propagation or cancellation support in this helper or in $fetch for server routes Forwarding Context & Headers. - The $fetch utility is based on ofetch, which does support the signal option, but the Nuxt docs do not explicitly show how to wire this up with request aborts in server API handlers. Summary:
The knowledge sources do not explicitly document how to propagate an abort signal from an outer API route to inner API calls in Nuxt. Your approach using event.node.req.on('close', ...) and passing an AbortController's signal to $fetch is a standard Node.js pattern, but it is not covered in the official Nuxt documentation provided above. Therefore, while your code is reasonable and aligns with general Node.js practices, there is no official Nuxt documentation or example confirming this pattern. If you need to ensure this works reliably, you may need to test it in your environment and consult the underlying ofetch documentation for signal support, as well as Node.js docs for request lifecycle events. In summary:
I'm sorry, but the knowledge sources do not contain enough information to confirm or document the propagation of abort signals through API routes in Nuxt as described in your example. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@558317900943982593> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?