I’ll refer to this great article about fetching with SSL in Node.js: Client Side SSL in node.js with fetch | Blog
The fetch logic is implemented in the same way, but in the Next.js API route. Locally with ‘next dev’ and ‘netlify dev’ it works fine. But when deployed to Netlify, the API route always fails with “Task timed out after 10.02 seconds”. It appears that the request is not being made or fails silently. Logs does not show anything.
I’m completely clueless now and so I’m asking for help. I hope someone has an idea why this is happening… maybe someone has faced a similar problem.
Request is being made and timing out in 10 seconds which is the limit for serverless functions to run on Netlify. On pro and above accounts, we can increase this to 26 seconds.
I understand, but in this case I don’t think it would be a problem. The server response is actually fast and the request is completed locally in less than 500ms (to the actual remote server). When it is deployed to Netlify, the request doesn’t seem to go through at all and I have no idea what could be the reason.
Unfortunately, I am unable to submit the code for testing as it contains 3rd party API keys and certificates. At the moment I don’t know any other way (service) to replace them for testing purposes. But the route handler code does not differ from the one mentioned in the article.
We would need to have some kind of reproduction to test this.
Additionally, you could use console.time()
and console.timeEnd()
in various stages in your API route to find out what could be taking long.
I will try to find a way to test it further. At the moment I know that the execution stops as soon as it reaches fetch()
. After that, there is no response or error.
Could you try to add a return
keyword before the fetch
?
Time logs show less than 800ms total for reading certificate and key, https.Agent
instantiation. And if return
before fetch()
, the request completes successfully.
Isn’t that expected behaviour then?
Yes, it is.
I’m confused because I don’t know how else to figure out what exactly happens with the fetch()
call. As I already mentioned, everything works correctly in local dev environment and with exact same keys.
Aa… now I see what you meant by the ‘return’ keyword before ‘fetch()’. I tried (admittedly not such a smart idea ) to return the data from the line above.
Anyway, I’ve now tested with the same request to a different endpoint (a simple Netlify Function handler) - the request completes successfully. I now understand that the problem is probably a server-specific API.
I will continue to investigate the issue, but as far as I can see, it is not with the Next.js API handler function or the Netlify environment.
Sorry for taking up your time. I think we can close this topic.