nuxt / nuxt

The Intuitive Vue Framework.

Home Page:https://nuxt.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Timeout timer invalidate in useFetch.

KyleXie opened this issue · comments

Summary (generated):

The speaker mentions a potential bug in the Nuxt framework. After a request is made, the timeout timer is not being cleared, which could cause it to trigger prematurely even if the request completes in time. Additionally, since the controller is a global variable, the timeout timer from a previous request could accidentally cancel the next request while it is still ongoing.


I'm not sure if it's a bug here. I see the timeout timer is not being cleared after the request, so it'll trigger even if the request gets a response in time. It could happen that the timeout timer of a previous request may trigger while the next request is ongoing if we call the refresh function of useFetch, so it may accidentally cancel the next request.

let controller: AbortController
const asyncData = useAsyncData<_ResT, ErrorT, DataT, PickKeys, DefaultT>(key, () => {
controller?.abort?.()
controller = typeof AbortController !== 'undefined' ? new AbortController() : {} as AbortController
/**
* Workaround for `timeout` not working due to custom abort controller
* TODO: remove this when upstream issue is resolved
* @see https://github.com/unjs/ofetch/issues/326
* @see https://github.com/unjs/ofetch/blob/bb2d72baa5d3f332a2185c20fc04e35d2c3e258d/src/fetch.ts#L152
*/
const timeoutLength = toValue(opts.timeout)
if (timeoutLength) {
setTimeout(() => controller.abort(), timeoutLength)
}