Next.js Discord

Discord Forum

How to increase Next.js app API response timeout limit in Vercel?

Answered
Serbian Tricolour Hound posted this in #help-forum
Open in Discord
Serbian Tricolour HoundOP
Hello, everyone.
As I was trying to increase API response timeout limit in Vercel for next.js app, I met a problem.
First, I upgraded my Vercel plan to pro. Its default timeout limit is 15 secs. And I put this code into my tsx (client file)
export const maxDuration = 300;
But when I deployed it to live domain and call API, its default timeout is still 15 secs, not increased yet. How can I fix this problem? And what is function failover feature in Vercel? Is it also related to this problem? Hope to hear from you soon. Thank you.

I use next version 14.1 in this project.
Answered by Ray
try to export on the page.tsx where you use the action
View full answer

37 Replies

Serbian Tricolour HoundOP
Hi, @Ray I put that code line into the specific file that includes a function (which needs long response timeout limit). This code line should be in pages/api directory? What if I don't have such an api route in the next.js app?
Serbian Tricolour HoundOP
let me try, @Ray thanks
Serbian Tricolour HoundOP
It doesn't work well. Here is my project structure. You can see that chatgpt.ts. I put that code into that file. (of
'use server';
) Where should I put that code? And I think there is no pages folder structure. Hope to hear from you soon, @Ray Thank you
or simple put export const maxDuration = 300; to src/app/layout.tsx
it should set the max duration to 300s for all route
Serbian Tricolour HoundOP
Thank you, I will try
Serbian Tricolour HoundOP
I just tried it. But it doesn't work. It returns 504 timeout error after 15 secs (pro plan default timeout limit) when I call API in live domain (vercel). But when I tried to deploy codes to Hobby plan vercel account, I got an error, timeout limit must be between 1 and 10 in Hobby plan. It means vercel gets MaxDuration from my code, but why doesn't work?
@Ray Please let me know possible cases.
Serbian Tricolour HoundOP
I think server action.
Where can I make sure it? I don't have route.ts
@Serbian Tricolour Hound Where can I make sure it? I don't have route.ts
can you show the code on src/app/layout.tsx?
Serbian Tricolour HoundOP
sec
Serbian Tricolour HoundOP
Please check
I think Vercel gets maxDuration variable in my codes, but it doesn't work. timeout error is issued in 15 secs
@Serbian Tricolour Hound Please check
try to export on the page.tsx where you use the action
Answer
Serbian Tricolour HoundOP
export the API (function) in chatgpt.ts, and import it in ChatWidget.tsx. I have to export maxDuration in page.tsx for global apply?
Serbian Tricolour HoundOP
sorry, I don't get it
where do you use the server action?
Serbian Tricolour HoundOP
chatgpt.ts
`use server`;

export const getAnswer = async () => {};
lib/api/chatgpt.ts
What you need more?
where do you call getAnswer?
Serbian Tricolour HoundOP
ChatWidget.tsx
where do you render ChatWidget
Serbian Tricolour HoundOP
give me a moment, let me check please
Serbian Tricolour HoundOP
import getAnswer method from chatgpt.ts to ChatWidget/index.tsx.
import ChatWidget/index.tsx in app/(main)/chat/page.tsx
Serbian Tricolour HoundOP
I try now
Serbian Tricolour HoundOP
Cool, it works
I appreciate your help, @Ray
@Serbian Tricolour Hound Cool, it works
np, look like setting the maxDuration on layout.tsx doesn't work