javascriptnext.jstimeoutserverlessvercel

How to increase timeout limit on Vercel Serverless functions


I have an API endpoint on a NextJS project that needs longer than 60s to run. I'm on a pro Vercel plan but for some reason cannot get the timeout limit to increase.

At the endpoint itself I've tried

export const maxDuration = 300
export const dynamic = 'force-dynamic'

which doesn't seem to do anything, I also tried adding a vercel.json file at the top level (above /src) like so:

{
    "functions": {
        "pages/api/**": {
            "memory": 3008,
            "maxDuration": 300
        },
    }
}

Which again isn't working. I've combed through the documentation (mostly here) and also a handful of threads (one example), none of which have helped.

I'm running NextJs version 13.5.6, am definitely on a pro plan, and Node v18, what am I doing wrong? Am really unsure of what else to try.


Solution

  • What you've done seems to be a mix of the Pages Router way the and the App Router way of doing it. Try to do it like in this example below:

    With Pages Router (/pages/api/my-function/handler.ts):

    export const config = {
      maxDuration: 5, // 5 seconds
    };
    
    export default function handler(
      request: NextApiRequest,
      response: NextApiResponse,
    ) {
      response.status(200).json({});
    }
    

    With App Router (/app/api/my-function/route.ts):

    export const maxDuration = 5; // 5 seconds
    export const dynamic = 'force-dynamic';
    
    export function GET(request: Request) {
      return new Response('{}', { status: 200 });
    }
    

    If you want to override the maximum duration of a function for your whole project when using Vercel, you can do it in vercel.json (I think this is only valid for the App Router):

    {
      "functions": {
        "app/api/**/*": {
          "maxDuration": 5
        }
      }
    }
    

    You can read more about all this on the official docs.