Next.js API Route Timeouts
My API routes timeout after 10 seconds on Vercel. How to fix?
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.
Can I increase the timeout on self-hosted Next.js?
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;`
What's the best pattern for long-running operations?
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.
Can I use WebSockets for real-time updates instead?
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.