Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A Next.js route handler is cut off after a fixed time

ErrorRoute handler stops after a fixed number of seconds on production but not locally

Short answer

Next.js does not enforce a timeout itself. The limit comes from where it runs. On Vercel, export maxDuration from the route file, within your plan's limit. When you self-host, maxDuration changes nothing: the cut-off is usually the reverse proxy, such as nginx proxy_read_timeout at 60 seconds.

  • Applies to Next.js 15, Next.js 16, Vercel with Fluid compute, nginx 1.26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Time the failing request. If it dies at a round number such as 10, 60 or 300 seconds, that number is a platform or proxy limit, and it tells you which cause applies.

    time curl -s -o /dev/null -w "%{http_code}\n" https://example.com/api/report
  2. 2Match what you see, then jump to its fix

    1. 1The request ends at your platform's default durationmaxDuration is not exported on the route (Vercel)Fix
    2. 2maxDuration is set above your plan limit, and the function still stops at the plan maximumThe value is above what your Vercel plan allowsFix
    3. 3The request dies near 60 seconds behind nginx, and the handler keeps no limit of its ownSelf-hosted: nginx closes the connection firstFix

What does the error look like?

  • Route handler stops after a fixed number of seconds on production but not locally
  • Long request returns a gateway timeout from nginx

Cause 1 of 3

maxDuration is not exported on the route (Vercel)

1Check

Next.js documents maxDuration as the maximum execution time in seconds for a route segment, and says deployment platforms can use it from the build output.

grep -rn "maxDuration" app

2Fix

Export it from the route file. For Server Actions, export it at page level to change the timeout of all actions on that page.

// app/api/report/route.ts
export const maxDuration = 60

export async function GET() {
  // long work
  return Response.json({ ok: true })
}

3Verify

Redeploy and repeat the slow request.

time curl -s -o /dev/null -w "%{http_code}\n" https://example.com/api/report

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 3

The value is above what your Vercel plan allows

1Check

Vercel's table for functions with Fluid compute lists a default of 300 seconds on all plans, a maximum of 300 seconds on Hobby and 800 seconds on Pro and Enterprise.

npx vercel inspect <deployment-url> --json

2Fix

Lower maxDuration to the plan maximum. You can also set a project-wide default in vercel.json.

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

3Verify

The inspect output lists each function's timeout; it should show your value.

npx vercel inspect <deployment-url> --json

Confirmed by vercel.com

Did this fix it?

Cause 3 of 3

Self-hosted: nginx closes the connection first

1Check

nginx proxy_read_timeout defaults to 60 seconds. It counts the silence between two reads from the app, so a handler that sends nothing for a minute is closed.

grep -rn "proxy_read_timeout" /etc/nginx/

2Fix

Raise it for that location, or stream progress so the connection is never silent.

location /api/report {
    proxy_pass http://127.0.0.1:3000;
    proxy_read_timeout 300s;
}

3Verify

Test the config, reload nginx, and repeat the slow request.

sudo nginx -t && sudo systemctl reload nginx

Confirmed by nginx.org

Did this fix it?

How do I stop it coming back?

  • Set maxDuration on every route that calls a slow third-party API.
  • Move jobs that take minutes to a queue and return a job id.
  • Keep proxy and platform timeouts longer than the slowest route you expect.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

The browser blocks your API response: no Access-Control-Allow-Origin header

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.