Repository navigation
help detecting client disconnections in route handlers #48682
Replies: 6 comments 6 replies
|
maybe we can add an then in my route handler: would something like this work? |
|
maybe next should expose the original node request, nuxt 3 does this: event.node.req |
|
I have a similar use case, where I am streaming events to the client. When the user navigates away from the page, the event stream remains opened indefinitely on the server. |
|
Why this is closed ? Lets take example this Route Handler: export async function GET(req: NextRequest) {
const abortController = new AbortController()
// How to fire the abortController.abort() when this request is aborted by the caller
// to prevent further useless data processing
// This could be something like: req.on("abort",()=>{abortController.abort()})
try {
const url = '...'
const response = await axios.get(url, { signal: abortController.signal })
// do some processing using the data from axios here....
return NextResponse.json({ result: 'Some data...' }, { status: 200 })
} catch (error) {
if (axios.isCancel(error)) return NextResponse.json({ result: 'request canceled' }, { status: 499 })
return NextResponse.json({ error: 'Server error' }, { status: 500 })
}
} |

31d2b72