Next.js Discord

Discord Forum

Endpoint code not reliably executing when POST sent to it - only in production environment on Vercel

Unanswered
Amur catfish posted this in #help-forum
Open in Discord
Amur catfishOP
Summary:
My webapp receives a POST to api/training-completed, then the code executes based on the data in the post request. I've tested this many times in localhost / ngrok and the code executes perfectly - this problem only appears when I'm testing it in the full production environment on Vercel

The problem is that the code either executes or not depending on whether the user is actively on the webapp refreshing the page. If the user is refreshing the webapp as the POST is received, then the code executes and all is good.

BUT, if the user is not on the webapp and not actively refreshing the page (most of the time), then when the POST request is received, the code at the endpoint /api/training-completed does not execute and throws an error.

I'm thinking the issue may be with the cold start of the code at /api/training-completed? Or perhaps its the cold start with the prisma and planetscale database connection?

I've implemented cron jobs which ping /api/training-completed every minute to try and keep them warm, but this does not seem to work.

Here is the code snippet from /api/training-completed:
"
console.log('Training-Completed Request received', req.body);
await prisma.$connect();
if (req.body && req.body.ping === "keepWarm") {
console.log('WARM!- training-completed');
return res.status(200).send("Training Completed Endpoint is warm!");
}
if (req.method !== "POST") {
res.setHeader("ALLOW", "POST");
return res.status(405).end(Method ${req.method} not allowed);
}
try {
await retry(async () => {
const user = await prisma.user.update({
where: {
id: userId,
},
data: {
trainerVersion: req.body.version,
},
});
},
{
retries: 10,
minTimeout: 500,
factor: 1.5,
onRetry: (error) => {
console.log('Retry after database timeout error:', error.message);
}
});
res.status(200).send("successfully received trainerVersion");
"

0 Replies