Back to Tech Q&A
Tech Q&A

Next.js API Route Timeouts

Dec 8, 2024
Next.jsVercelAPITimeouts
Question

My API routes timeout after 10 seconds on Vercel. How to fix?

Answer

Vercel's Hobby plan has a 10s limit. Upgrade to Pro for 60s, or move long-running tasks to background jobs using Vercel's cron or edge functions. You can also use streaming responses to keep the connection alive.

Question

Can I increase the timeout on self-hosted Next.js?

Answer

Yes, set the `maxDuration` export in your route file. For self-hosted, you can also configure your reverse proxy (nginx/Apache) timeout settings. Example: `export const maxDuration = 60;`

Question

What's the best pattern for long-running operations?

Answer

Use a job queue pattern: API returns immediately with a job ID, process async, client polls or uses webhooks for completion. Libraries like BullMQ or Inngest make this easy.

Question

Can I use WebSockets for real-time updates instead?

Answer

Not directly in API routes—they're serverless. Use a separate WebSocket service (Pusher, Ably) or deploy a custom server. Server-Sent Events (SSE) work in some cases as an alternative.