How to increase Next.js app API response timeout limit in Vercel?
Answered
Serbian Tricolour Hound posted this in #help-forum
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)
I use next version 14.1 in this project.
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.
37 Replies
@Serbian Tricolour Hound 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.
where do you put this line?
try adding it in the api route
export const maxDuration = 300;try adding it in the api route
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 Hound Hi, <@743561772069421169> 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?
it should be exported from a layout.tsx, page.tsx or route.tsx
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@Serbian Tricolour Hound 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, <@743561772069421169> Thank you
where do you use the function export in chatgpt.ts?
or simple put
export const maxDuration = 300; to src/app/layout.tsxit 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 Hound 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?
are you using server action or route handler?
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
import ChatWidget/index.tsx in app/(main)/chat/page.tsx
@Serbian Tricolour Hound import getAnswer method from chatgpt.ts to ChatWidget/index.tsx.
import ChatWidget/index.tsx in app/(main)/chat/page.tsx
ok export the maxDuration on this 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