My website became really slow after deploying to Netify and I have no idea why. It is rather obvious that response times can be slower due to geographical location and distances between servers but in my case the functions’ execution time became 3-5 seconds longer.
Code of the example function (next.js api endpoint):
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<ExampleAnswerApiResponse>
) {
if (req.method === 'POST') {
const start = performance.now()
console.log("Starting processing...")
try {
console.log(`Getting session (${performance.now() - start})`)
const session = await getServerSession(req, res, authOptions)
if (!session)
return res.status(401).json({ success: false, code: "UNAUTHORIZED" });
console.log("Got session: ", session.user.name, "(", performance.now() - start, ")")
console.log(`Starting porcessing body (${performance.now() - start})`)
const body = await JSON.parse(req.body)
const type = body.type
if (!type || (type !== "answering_pois" && type !== 'asking_pois' && type !== 'speech')) return res.status(400).json({ success: false, code: "DATA_MISSING" })
console.log(`Got body: type = ${type} (${performance.now() - start})`)
console.log(`Getting answer id and example answer (${performance.now() - start})`)
const answer = await db.answer.findFirstOrThrow({
select: {
id: true,
question: {
select: {
exampleAnswer: true
}
}
},
where: {
userId: session.user.id,
finished: false,
question: {
type: type
}
}
})
console.log(`Got answer: answer_id = ${answer.id} (${performance.now() - start})`)
console.log(`Updating answer (${performance.now() - start})`)
const r = await db.answer.update({
data: {
seenExampleAnswer: true
}, where: {
id: answer.id
}
})
console.log(`Updated answer: ${r.id} (${performance.now() - start})`)
console.log(`returning (${performance.now() - start})`)
return res.status(200).json({
success: true,
code: null,
exampleAnswer: answer.question.exampleAnswer
})
} catch (err) {
console.log(err)
return res.status(500).json({
success: false,
code: "UNKNOWN_ERROR"
})
}
} else {
return res.status(405).send({ success: false, code: 'INVALID_METHOD' })
}
}
On development server and db:
On development server and production db:
On netlify using the same production db as above:
(All timestamps are in ms)
Queries execution time in production db is around 10ms (planet scale):
Additionally I have no idea why getServerSession is suddenly taking over 1s on netlify when it theoretically doesn’t make any requests.
Also there is over 1s gap between last console.log and end of execution (console.log - 2505ms, total function duration - 3818ms)
website: https://mywebsite1234abvjkjsdf.netlify.app
I’ve been stuck on this for the past few days so I would really appreciate any help