最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to increase timeout limit on Vercel Serverless functions - Stack Overflow

programmeradmin0浏览0评论

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 bed 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.

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 bed 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.

Share Improve this question asked Nov 17, 2023 at 18:04 Eric HasegawaEric Hasegawa 6691 gold badge11 silver badges29 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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.

发布评论

评论列表(0)

  1. 暂无评论