How do I extend fetch's default timeout?
Unanswered
Brokenwind posted this in #help-forum
I have an API that would take more than 5 minutes to process and get a response. After 300 seconds, I will get the error:
I'm using 14.2.8 App Router
UND_ERR_CONNECT_TIMEOUT
because the fetch is using undici which is the cause if my understanding is correct. How can I bypass this to have a longer timeout or just even remove that entirely?I'm using 14.2.8 App Router
72 Replies
Are you hosting through vercel?
@Brokenwind
@Jboncz Are you hosting through vercel?
No, I'm hosting locally and this project will just run through LAN
Gotcha, one sec ill link you the article on how to do it
I know I have seen this option 😂
looking for it
export const maxDuration = 5
No worries and take your time. I've seen articles on how to extend undici's but not Next's fetch
Is this in ms?
if you want it to propogate all the way down
Uhhhhh you would thinkk... but 5 makes me think not
lol
Give it a shot whats the worst than can happen 😄
Not at my computer
Hol up let me try this
export const maxDuration = 5; // This function can run for a maximum of 5 seconds
Its seconds
https://vercel.com/docs/functions/configuring-functions/duration
I know this says for vercel.... but if you look at specifically the runtime segment it says to use what I said
I know this says for vercel.... but if you look at specifically the runtime segment it says to use what I said
I started the process now. I've set it to 2 hours so I'll just wait if it takes more than 5 mins
ye
its for a route segment... soooo, if you put it in the root layout itll propogate all the way down to everything
Would this work for fetch's own timeout?
Does fetch have its own timeout?
I dont think it does?
Yeah the 5 minute one from undici
Why you use that? lol
That's also what I need to be extended
just for the ease of use? Ive never heard of it till now tbh
Next is using undici in its wrapper for fetch, I believe. It times out after 5 minutes.
Nah, fetch doesnt use another libraries wrapper as far as I know
in nextjs*
are you just using
fetch()
in your code or using import { fetch, Agent } from 'undici'
const res = await fetch('https://example.com')
Check this one:
https://github.com/vercel/next.js/discussions/57384
They said to remove the undici in Next 14 but comments are saying they still have the same error, and I do have the same error.
https://github.com/vercel/next.js/discussions/57384
They said to remove the undici in Next 14 but comments are saying they still have the same error, and I do have the same error.
Right, your hitting the default fetch timout. They dont use another libraries fetch wrapper
Did your request time out?
Yup, 5 minutes is really the max.
⨯ TypeError: fetch failed
at async fetcher (./src/utils/fetcher.js:13:22)
at async copyMIMSQuota (./src/actions/mims-quota.js:86:22)
digest: "2099972895"
Cause: HeadersTimeoutError: Headers Timeout Error
at Timeout.onParserTimeout [as callback] (node:internal/deps/undici/undici:5980:32)
at Timeout.onTimeout [as _onTimeout] (node:internal/deps/undici/undici:2332:17)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7) {
code: 'UND_ERR_HEADERS_TIMEOUT'
}
I might ditch fetch for axios if I don't find a workaround. Based on the error code, they are still using undici.
export const maxDuration = 2 * 60 * 60;
I think this is on the fetching part
Nah I promise ive done this before lol
If I understand this correctly, the native fetch doesn't have any timeout. Only Next has this 5 minute timeout due to undici.
Try this.
module.exports = {
server: {
maxDuration: 300000, // 300 seconds (5 minutes)
},
}
in your next.config.js
I'll try it.
Ill try it too lol back at my pc
Sorry, I just looked at my changelog for how I overcame the 5 minutes.
Are you using nginx or something similar
Yes for Nginx
Ahhhh let me pull my nginx config
thats the issue
it has a default
# Extend timeouts
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
Although I'm only using Nginx for reverse proxy. I can try whatever helps before I install axios 💀
Yep.
Its this I give it a 99% chance
that goes in the http block
I use nginx as y reverse proxy at work as well
@Brokenwind I would follow my screenshot. the code block above was from stack overflow lol
idk if s works or not I just did it in milliseconds
use
then
nginx -t
to testthen
systemctl reload nginx
Got it. I'll test these within the day. Thank you so much for the help!
np make sure to check in and let me know!
always best to avoid dependencies where you can