Nest.js - increase max duration timeout

Hi there!

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:

{
  "version": 2,
  "builds": [
    {
      "src": "src/main.ts",
      "use": "@vercel/node"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "src/main.ts"
    }
  ]
}

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.

Thanks in advance!

Hi @yyosifov!

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');
}

Hi @pawlean!

Thanks for the suggestion. I’ve tried adding this to several places:

  1. Added to main.ts - where the bootstrap() of the nest.js is done. It breaks the whole application with an error like this one:
Invalid export found in module "/var/task/backend/src/main.js".
The default export must be a function or server.

With the other options the application works, but the maxDuration is not applied:

  1. Added to app.module.ts which is the entry module of the nest.js app → It doesn’t work
  2. Added to test.module.ts which is the module with the test controller I test with - doesn’t work
  3. Added to test.controller.ts which is the controller I test with

The test is pretty simple - a promise resolved after 25 seconds:

  @Get('/test-timeout')
  async testTimeout() {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve('done');
      }, 25000);
    });
  }

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 in advance for your kind help!

Best,
Yosif

Tagging @eben-bealcoza here, they recently shared that they increased their timeout for their project. I wonder if they may have some insights!

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:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // Middleware to simulate maxDuration locally
  app.use((req, res, next) => {
    req.setTimeout(30000); // 30 seconds
    next();
  });

  await app.listen(3000);
}
bootstrap();

Check the relevant Nest.js documentation.

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?

{
  "functions": {
    "src/main.ts": {
      "runtime": "@vercel/node",
      "maxDuration": 30
    }
  }
}

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.

2 Likes

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.

{
“functions”: {
“app/api/**/*”: {
“maxDuration”: 60
}
}
}

This resolved my issue. Hope this helps.

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.