From 69430aa26c88b341465ae5d7812b2611e86888ab Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 22 May 2024 16:00:03 +0100 Subject: [PATCH] fix(nuxt): clear timeout when asyncData request finishes/aborts (#27308) --- packages/nuxt/src/app/composables/fetch.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index e06ec23309..a083c47789 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -164,8 +164,10 @@ export function useFetch< * @see https://github.com/unjs/ofetch/blob/bb2d72baa5d3f332a2185c20fc04e35d2c3e258d/src/fetch.ts#L152 */ const timeoutLength = toValue(opts.timeout) + let timeoutId: NodeJS.Timeout if (timeoutLength) { - setTimeout(() => controller.abort(), timeoutLength) + timeoutId = setTimeout(() => controller.abort(), timeoutLength) + controller.signal.onabort = () => clearTimeout(timeoutId) } 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) return asyncData