fix(nuxt): clear timeout when asyncData request finishes/aborts (#27308)

This commit is contained in:
Daniel Roe 2024-05-22 16:00:03 +01:00 committed by GitHub
parent 811bfc18a1
commit 69430aa26c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -164,8 +164,10 @@ export function useFetch<
* @see https://github.com/unjs/ofetch/blob/bb2d72baa5d3f332a2185c20fc04e35d2c3e258d/src/fetch.ts#L152 * @see https://github.com/unjs/ofetch/blob/bb2d72baa5d3f332a2185c20fc04e35d2c3e258d/src/fetch.ts#L152
*/ */
const timeoutLength = toValue(opts.timeout) const timeoutLength = toValue(opts.timeout)
let timeoutId: NodeJS.Timeout
if (timeoutLength) { if (timeoutLength) {
setTimeout(() => controller.abort(), timeoutLength) timeoutId = setTimeout(() => controller.abort(), timeoutLength)
controller.signal.onabort = () => clearTimeout(timeoutId)
} }
let _$fetch = opts.$fetch || globalThis.$fetch let _$fetch = opts.$fetch || globalThis.$fetch
@ -178,7 +180,7 @@ export function useFetch<
} }
} }
return _$fetch(_request.value, { signal: controller.signal, ..._fetchOptions } as any) as Promise<_ResT> return _$fetch(_request.value, { signal: controller.signal, ..._fetchOptions } as any).finally(() => { clearTimeout(timeoutId) }) as Promise<_ResT>
}, _asyncDataOptions) }, _asyncDataOptions)
return asyncData return asyncData