Side effects in serverless route handlers
Answered
Turkish Van posted this in #help-forum
Turkish VanOP
I've deployed my nextjs app on vercell, and thus all the API route handlers work in a serverless fashion.
Some of these handlers process webhook calls, the payload of which requires some processing, which could take a few seconds. And I want to immediately send back a 200 to the webhook provider to notify them so that they don't keep retrying.
So the way I'm doing this is, after verifying the webhook payload, I call another next.js route handler API using fetch, offload the processing to that route, and then return a 200.
This works fine in local development when it's pretty much a stateful node backend continuously running.
But when deployed on the edge, the fetch call to the other route handler never gets made. Sometimes I get fetch failed error, while sometimes I don't get any error, but I just can't see the other endpoint being hit at all (in any case).
How can I resolve this situation?
Some of these handlers process webhook calls, the payload of which requires some processing, which could take a few seconds. And I want to immediately send back a 200 to the webhook provider to notify them so that they don't keep retrying.
So the way I'm doing this is, after verifying the webhook payload, I call another next.js route handler API using fetch, offload the processing to that route, and then return a 200.
This works fine in local development when it's pretty much a stateful node backend continuously running.
But when deployed on the edge, the fetch call to the other route handler never gets made. Sometimes I get fetch failed error, while sometimes I don't get any error, but I just can't see the other endpoint being hit at all (in any case).
How can I resolve this situation?
Answered by Turkish Van
apparently waituntil is what I'm after @Pygmy Nuthatch
https://vercel.com/docs/functions/functions-api-reference#waituntil
I believe what happened here is, before the asynchronous fetch actually register and any data for the request gets sent, the main API function already returned a response and stopped the execution completely.. thus the fetch never actually getting called.. and waituntil helps solve for such use cases
https://vercel.com/docs/functions/functions-api-reference#waituntil
I believe what happened here is, before the asynchronous fetch actually register and any data for the request gets sent, the main API function already returned a response and stopped the execution completely.. thus the fetch never actually getting called.. and waituntil helps solve for such use cases
14 Replies
Pygmy Nuthatch
The server doesn’t know relative routes do you have the url correct
?
@Pygmy Nuthatch The server doesn’t know relative routes do you have the url correct
Turkish VanOP
Yeah. I'm using the host from the request headers
Logged the url being used, and it's as intended
Pygmy Nuthatch
Have you tryed calling the logged url in postman
Othervise i think im out of answers
@Pygmy Nuthatch Have you tryed calling the logged url in postman
Turkish VanOP
I just tried doing it and it works fine
the thing is in vercel logs
i dont even see any requests to the logged URL
it seems like the call isnt being made from one route to another
Turkish VanOP
apparently waituntil is what I'm after @Pygmy Nuthatch
https://vercel.com/docs/functions/functions-api-reference#waituntil
I believe what happened here is, before the asynchronous fetch actually register and any data for the request gets sent, the main API function already returned a response and stopped the execution completely.. thus the fetch never actually getting called.. and waituntil helps solve for such use cases
https://vercel.com/docs/functions/functions-api-reference#waituntil
I believe what happened here is, before the asynchronous fetch actually register and any data for the request gets sent, the main API function already returned a response and stopped the execution completely.. thus the fetch never actually getting called.. and waituntil helps solve for such use cases
Answer
@Turkish Van apparently waituntil is what I'm after <@1118541904653582387>
https://vercel.com/docs/functions/functions-api-reference#waituntil
I believe what happened here is, before the asynchronous fetch actually register and any data for the request gets sent, the main API function already returned a response and stopped the execution completely.. thus the fetch never actually getting called.. and waituntil helps solve for such use cases
Pygmy Nuthatch
Cool, thought it could have been something like that but my js knowledge wasn’t that deep
@Pygmy Nuthatch Cool, thought it could have been something like that but my js knowledge wasn’t that deep
Turkish VanOP
No worries man I just got to learn about it too. I appreciate you trying to help