Every time I open my web app after a while, the first request always returns 504 Gateway Timeout error. However, the immediate subsequent request return data with status 200. This is true for almost every API route. The error log on vercel looks like:
GET] /api/villages?subdistrict=4231
12:14:52:06
Function Status: None
Edge Status: 504
Duration: 10010.66 ms
Init Duration: N/A
Memory Used: 118 MB
ID: rq8d4-1628059491869-fecd750a64de
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62
2021-08-04T06:45:02.214Z 10867116-4d89-4559-b976-14418b6f5102 Task timed out after 10.01 seconds
I first suspected that the error is with MongoDB connection, however it works just fine if I build the project on my local machine. The waiting period is always below 500ms.
I don't know how to solve or even diagnose this issue as the site is already in production, and the error occurs only when deployed on Vercel. Please help me solve the issue.