I am hosting a Nest.js backend application on Vercel - for ease of deployment and to have everything in one place.
Some of my routes take more than 10s and therefore hit the maximum duration limit. I’ve tried increasing the limit as per the docs: Configuring Maximum Duration for Vercel Functions but it doesn’t work out. I’m using a vercel.json configuration similar to this:
I cannot add “functions” section as the configuration becomes invalid, because of the presence of the build section. I’ve tried to “export const maxDuration = 50” at multiple places in my Nest.js application, but it either breaks or doesn’t take any effect.
Is there some workaround - I.e. how can I increase the limit of the function invocation for the whole backend or certain routes?
Even if I upgrade my plan, the default would be 15s, which I won’t be able to increase further.
Seeing as you need to use both the builds and functions properties together in your vercel.json configuration, you’re right - you will need to approach it differently since Vercel does not support using both properties in combination.
You already mentioned trying to use a Function File. What does it look like? This is an example configuration you could try out:
export const config = {
maxDuration: 30 // Sets the maximum duration to 30 seconds
};
export default async function handler(req, res) {
// Your function logic here
res.status(200).send('Function executed');
}
Locally it works. When deployed - it timeouts after 10s.
Is there a way I can test it locally somehow? I don’t have a typical handler function in a serverless fashion, rather I have Nest.js controllers and I wonder how can I push this maxDuration to the environment to be respected.
Thanks for the detail! I’d love to test this out with a minimal reproducible example. I sometimes find that stripping down the application to the bare minimum helps debug problems better.
Also, have you got any logs that may give you some helpful pointers?
For local testing, you could simulate the serverless environment by setting the maxDuration in your local server configuration. A middleware could handle this, similar to:
As far as I know exporting maxDuration won’t work as we don’t support it in Nest.js.
I haven’t tested this with Nest.js, but it might be possible to do something like this in vercel.json?
Alternatively you can now configure the default project function timeout.
For this head to your Project in the Dashboard and look under Settings > Functions:
Please note that this can impact your billing. Keep the default duration as low as possible to ensure your function execution will be terminated when you have hanging promises or other reasons your code takes a long time to run.
Sorry for the late reply. … . This is exactly what I did, I tried the config on the dashboard but eventually I added vercel.json to the project with the below config. Not optimised but it is a start.