From 596ae29d34b2f3212ad44a8862b81042e0f093e5 Mon Sep 17 00:00:00 2001 From: Alex Liu Date: Mon, 11 Mar 2024 19:31:32 +0800 Subject: [PATCH 1/4] refactor: simplify `request` computation with `toRef` --- packages/nuxt/src/app/composables/fetch.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index f907e6d2aed2..420166083c01 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -1,7 +1,7 @@ import type { FetchError, FetchOptions } from 'ofetch' import type { NitroFetchRequest, TypedInternalResponse, AvailableRouterMethod as _AvailableRouterMethod } from 'nitropack' import type { MaybeRef, Ref } from 'vue' -import { computed, reactive, toValue } from 'vue' +import { computed, reactive, toValue, toRef } from 'vue' import { hash } from 'ohash' import { useRequestFetch } from './ssr' @@ -94,13 +94,7 @@ export function useFetch< ) { const [opts = {}, autoKey] = typeof arg1 === 'string' ? [{}, arg1] : [arg1, arg2] - const _request = computed(() => { - let r = request - if (typeof r === 'function') { - r = r() - } - return toValue(r) - }) + const _request = toRef(request) const _key = opts.key || hash([autoKey, typeof _request.value === 'string' ? _request.value : '', ...generateOptionSegments(opts)]) if (!_key || typeof _key !== 'string') { From 4696832d286774721aa78ab345bb0a90d12eea5a Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 11 Mar 2024 05:14:07 -0700 Subject: [PATCH 2/4] chore: remove unused import --- packages/nuxt/src/app/composables/fetch.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index 420166083c01..3084c3682782 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -1,7 +1,7 @@ import type { FetchError, FetchOptions } from 'ofetch' import type { NitroFetchRequest, TypedInternalResponse, AvailableRouterMethod as _AvailableRouterMethod } from 'nitropack' import type { MaybeRef, Ref } from 'vue' -import { computed, reactive, toValue, toRef } from 'vue' +import { reactive, toValue, toRef } from 'vue' import { hash } from 'ohash' import { useRequestFetch } from './ssr' From 3600eca5c9971dc0e57a8c196378e029e4124f41 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 11 Mar 2024 12:16:28 +0000 Subject: [PATCH 3/4] [autofix.ci] apply automated fixes --- packages/nuxt/src/app/composables/fetch.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index 3084c3682782..408c2bff4e25 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -1,7 +1,7 @@ import type { FetchError, FetchOptions } from 'ofetch' import type { NitroFetchRequest, TypedInternalResponse, AvailableRouterMethod as _AvailableRouterMethod } from 'nitropack' import type { MaybeRef, Ref } from 'vue' -import { reactive, toValue, toRef } from 'vue' +import { reactive, toRef, toValue } from 'vue' import { hash } from 'ohash' import { useRequestFetch } from './ssr' From e2f403bb26785dc11bea6c4c3c1e107072f1564b Mon Sep 17 00:00:00 2001 From: Alex Liu Date: Tue, 12 Mar 2024 00:09:15 +0800 Subject: [PATCH 4/4] refactor(nuxt): use more balanced approach --- packages/nuxt/src/app/composables/fetch.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index 408c2bff4e25..4403415c2f52 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -1,7 +1,7 @@ import type { FetchError, FetchOptions } from 'ofetch' import type { NitroFetchRequest, TypedInternalResponse, AvailableRouterMethod as _AvailableRouterMethod } from 'nitropack' import type { MaybeRef, Ref } from 'vue' -import { reactive, toRef, toValue } from 'vue' +import { computed, reactive, toValue } from 'vue' import { hash } from 'ohash' import { useRequestFetch } from './ssr' @@ -94,7 +94,7 @@ export function useFetch< ) { const [opts = {}, autoKey] = typeof arg1 === 'string' ? [{}, arg1] : [arg1, arg2] - const _request = toRef(request) + const _request = computed(() => toValue(request)) const _key = opts.key || hash([autoKey, typeof _request.value === 'string' ? _request.value : '', ...generateOptionSegments(opts)]) if (!_key || typeof _key !== 'string') {