help detecting client disconnections in route handlers #48682
-
|
(very) simplified code follows: so this route handler has a big problem, if a client, say a video html5 player, attempts to stream through by making its GET request to this endpoint things works BUT if the client leaves (browser close, tab close, curl closes, everything) the pipe from s3 will still be ongoing. I tried shaping the env with:
it did not matter, in this env did some more in depth verifications and indeed: this correctly destroys everything so I think the problem lies in the: |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 6 replies
-
|
maybe we can add an then in my route handler: would something like this work? |
Beta Was this translation helpful? Give feedback.
-
|
maybe next should expose the original node request, nuxt 3 does this: event.node.req |
Beta Was this translation helpful? Give feedback.
-
|
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. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
|
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 })
}
} |
Beta Was this translation helpful? Give feedback.
-
|
I cannot understand why is it closed ? |
Beta Was this translation helpful? Give feedback.
31d2b72