Type something to search...
Next.js maxDuration

Next.js maxDuration

Serverless execution environments generally enforce a hard ceiling on how long any single invocation is allowed to run — exceed it, and the platform kills the function regardless of whether it was about to finish. maxDuration is how you tell Next.js (and, through it, your deployment platform) what that ceiling should be for a specific route segment, rather than relying entirely on whatever the platform's default happens to be.

Basic Usage

export const maxDuration = 5;

The value is a number, in seconds. This is one of the simpler route segment config options in terms of API surface — a single number, no enum of named strings to choose between.

What This Actually Does — and Doesn't Do

Here's the detail worth being precise about: maxDuration is a signal Next.js passes through in its build output; it doesn't independently enforce anything at the framework level. Deployment platforms read this value from the build output and apply it as an actual execution limit on their side. If your platform ignores the value, or if you're self-hosting without a platform that consumes this signal, setting maxDuration alone accomplishes nothing on its own — it's a hint that specific hosting infrastructure needs to be listening for and honoring, not a guarantee Next.js itself enforces at runtime.

Practically, this means the exact behavior when a segment exceeds its configured duration — whether it's terminated at precisely that limit, whether there's a platform-specific default ceiling if you don't set this at all, what error the client actually sees — depends entirely on your specific deployment platform's own documentation, not on anything Next.js controls directly.

Server Actions Have a Special Rule Here

For Server Actions specifically, there's a scoping detail worth knowing: setting maxDuration at the page level changes the default timeout for every Server Action used on that page — not just for the page's own rendering. If a page has several distinct Server Actions attached to different forms or interactions, they all inherit that one page-level maxDuration value as their shared timeout ceiling, rather than each needing its own separate configuration. If different Server Actions on the same page genuinely need different timeout budgets, that's not something this single page-level export can express on its own — you'd need to structure those actions across different routes, or handle the finer-grained timeout logic inside the action itself rather than relying purely on this config option.

Version History

VersionChanges
v13.4.10maxDuration introduced

Key Takeaways

AspectDetail
Value typeA number, in seconds
EnforcementPassed through to your deployment platform — Next.js doesn't enforce it independently
Server ActionsA page-level maxDuration sets the default timeout for every Server Action on that page
Self-hostingOnly meaningful if your hosting setup actually reads and applies this build-output signal

maxDuration is a small but genuinely important knob for anything doing real work on the server — a slow third-party API call, a heavier data transformation — where the platform's default timeout might be either too generous (letting a hung request tie up resources longer than it should) or too strict (killing legitimate slow-but-successful work prematurely). Set it deliberately based on what your platform actually supports and enforces, rather than assuming a number here changes behavior on its own.

Tags :
Share :

Related Posts

Can Next.js Be Used with GraphQL?

Can Next.js Be Used with GraphQL?

Next.js and GraphQL are two powerful technologies that have gained significant traction in the web development community. Next.js, a React-based fram

Dive Deeper
How does Next.js differ from Create React App?

How does Next.js differ from Create React App?

In the world of modern web development, React.js has emerged as a dominant force due to its flexibility, performance, and extensive ecosystem. Two po

Dive Deeper
How does Next.js handle image optimization?

How does Next.js handle image optimization?

In modern web development, image optimization plays a critical role in enhancing user experience and improving site performance. Large, unoptimized i

Dive Deeper