Mendlark / Guides

Vercel 504 "FUNCTION_INVOCATION_TIMEOUT": how to fix it

The page spins for a while, then you get a 504 and FUNCTION_INVOCATION_TIMEOUT, or the logs say Task timed out. It worked on your laptop because nothing there stops a slow request. On Vercel, every server function has a time limit, and yours is running past it.

1. Find which call is slow

Open your project in Vercel, go to Logs, and filter by the failing route. A timed-out request shows how long it ran. Then look at what that function waits for. In AI-built apps it's nearly always one of these: a call to an AI model (OpenAI, Anthropic and similar), a database query, an outside API, or a loop that calls one of those many times.

2. Make sure the function always answers

A function that never sends a response runs until it times out, even if the work finished. Check every path, including errors: each one should return a response. A fetch to a URL built from a missing env variable can also hang, so confirm your production env variables are set (and redeploy after adding them).

3. Raise the time limit if the work is genuinely long

You can give a function more time, up to your plan's maximum (see Vercel's Functions limits docs for current numbers). In a Next.js route file:

export const maxDuration = 60; // seconds

For other frameworks, set it in vercel.json:

{ "functions": { "api/*.js": { "maxDuration": 60 } } }

Redeploy afterwards. If the limit is already at the maximum, raising it won't help: fix the slow work instead.

4. Stream AI responses instead of waiting for the whole answer

Long AI replies are the classic cause. Stream the response to the browser as it's generated (the Vercel AI SDK's streamText does this), so the user sees words straight away and you're not holding a silent request open.

5. Move long jobs out of the request

Generating reports, processing uploads or calling an API for hundreds of rows doesn't belong in a single page request. Save a "job" row, return straight away, do the work in a background job or queue, and let the page check back for the result.

6. Put the function near your database

If your Supabase database is in Europe and your functions run in the US, every query crosses the Atlantic. Set the function region close to the database in your Vercel project settings, and reuse one database client instead of opening a new connection per query.

Quick checklist

  • Slow route found in Vercel logs
  • Every code path returns a response, including errors
  • Production env variables set and redeployed
  • maxDuration set where the work is genuinely long
  • AI responses streamed, long jobs moved to the background
  • Function region matches the database region

Still stuck?

Mendlark fixes this kind of problem for a fixed €49 (launch price, normally €79). We agree the problem first, test the fix and send it as a pull request, so nothing changes in your app until you merge it. If we can't fix it, you don't pay. Get the €49 fix, or start with a free scan.

Not sure what else is wrong? Paste your app link into the free instant check and get a plain-English list of problems in seconds.