Works locally, 504 live? Function timeouts

You add a feature that calls an AI model, generates a PDF, imports a spreadsheet or sends a batch of emails. On your laptop it works every time. In preview it mostly works. On the live site, real users sometimes get a 504 Gateway Timeout, a generic "something went wrong" toast, or a spinner that never stops. Nothing is wrong with your code's logic. It is simply running longer than your host allows.

The symptom

  • The feature fails only on bigger inputs: long prompts, large files, many rows.
  • The browser network tab shows a 504, 502 or a request that is cut off after a fixed number of seconds.
  • Your function logs show the start of the request but no finish line, or a message like "Task timed out" or "function exceeded the time limit".

The cause

Sites built with Lovable, Bolt, Replit, v0 or Cursor usually run their server code as short-lived functions: Supabase Edge Functions, Vercel Functions, Netlify Functions, Cloudflare Workers or a Replit deployment. Each of these platforms sets a maximum run time per request, and the limit often depends on your plan. Your local dev server has no such limit, so a call that takes a long time locally simply finishes.

The usual culprits are calls to large AI models that stream or think for a while, loops that call another API once per row, file conversions, and work that waits on a slow third-party service. When the limit is hit, the platform kills the function mid-task. Any work after that point never happens, and the browser gets an error instead of your response.

Step-by-step diagnosis

  1. Time the failing request. Open the browser developer tools, go to the Network tab, trigger the feature and note how long the request runs before it fails. If failures always land at the same duration, that is almost certainly a platform limit.
  2. Read the function logs. Your host's dashboard shows logs per function. Look for timeout wording, or for a log line you print at the start with no matching line at the end.
  3. Find your real limit. Check your host's documentation for function duration limits on your current plan. Do not rely on a number you remember, as these limits change over time and differ by plan.
  4. Measure the slow part. Add a log line with a timestamp before and after each external call. Usually one call, often the AI model or a per-row loop, takes most of the time.

Fixes by builder and host

Lovable and Supabase Edge Functions

Keep each edge function short. For long jobs, have the function save a row in a jobs table and return right away with the job id. Do the slow work in a separate step, then let the page poll that row or subscribe to it with Supabase Realtime. Ask Lovable to "move this work into a background job with a status table" rather than to "make it faster".

v0, Cursor and Vercel

On Vercel you can raise a route's maximum duration with the maxDuration setting, up to what your plan allows. That buys time, but it is not a cure. For AI responses, stream the output to the browser so the user sees progress. For heavy work, hand it to a queue or a scheduled job and return a job id.

Bolt and Netlify

Netlify offers background functions for work that should keep going after the response is sent. Use one for imports, reports and bulk emails, and write the result somewhere the page can read later.

Replit

Check which deployment type you picked. A request-based deployment can cut off long requests, while an always-on server or a scheduled deployment suits long jobs better. Keep slow work off the request path either way.

Any host

  • Batch per-row API calls instead of making one call per row.
  • Set your own timeout on outside calls, shorter than the platform limit, so you can return a friendly error instead of a dead 504.
  • Show a clear message when a job is still running, not an endless spinner.

How to verify the fix

  1. Run the feature on the live site with your largest realistic input.
  2. Confirm the first response returns quickly, with either the result or a job id.
  3. Confirm the job finishes and the page shows the result without a refresh loop that never ends.
  4. Check the function logs for a clean finish line and no timeout message.

Prevention

  • Treat anything that might take more than a few seconds as a background job from day one.
  • Log start and end times for every function that calls an outside service.
  • Write down your host's duration limit for your plan next to your deploy notes, and recheck it when you change plans.
  • Keep an eye on the live site after each deploy, because these failures only show up with real traffic and real inputs.

Run the free 30-second check

Run the free 30-second check

← All posts

FlowCanary on SaaSHub