From 0e5b6315484b71f8cb799cb8bd1b57f13578cc28 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 15 Aug 2022 11:39:49 +0100 Subject: [PATCH 1/3] fix(nuxt): use url as fetch key if none other is provided --- packages/nuxt/src/app/composables/fetch.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index a1e8132cdcd..55efa740f35 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -1,6 +1,7 @@ -import type { FetchOptions } from 'ohmyfetch' +import type { FetchOptions, SearchParams } from 'ohmyfetch' import type { TypedInternalResponse, NitroFetchRequest } from 'nitropack' import { computed, isRef, Ref } from 'vue' +import { withBase, withQuery } from 'ufo' import type { AsyncDataOptions, _Transform, KeyOfRes, AsyncData, PickFrom } from './asyncData' import { useAsyncData } from './asyncData' @@ -38,7 +39,7 @@ export function useFetch< arg2?: string ) { const [opts = {}, autoKey] = typeof arg1 === 'string' ? [{}, arg1] : [arg1, arg2] - const _key = opts.key || autoKey + const _key = opts.key || typeof request === 'string' ? withBase(opts.baseURL || '', withQuery(request as string, opts.params as SearchParams)) : autoKey if (!_key || typeof _key !== 'string') { throw new TypeError('[nuxt] [useFetch] key must be a string: ' + _key) } From c9ef662152e5ca429552577640ebf94480de78f9 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 15 Nov 2022 16:32:22 +0000 Subject: [PATCH 2/3] fix: hash url key --- packages/nuxt/src/app/composables/fetch.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index 7680095d25b..5197460908d 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -2,6 +2,7 @@ import type { FetchError, FetchOptions } from 'ofetch' import type { TypedInternalResponse, NitroFetchRequest } from 'nitropack' import { computed, unref, Ref, reactive } from 'vue' import { withBase, withQuery } from 'ufo' +import { hash } from 'ohash' import type { AsyncDataOptions, _Transform, KeyOfRes, AsyncData, PickFrom } from './asyncData' import { useAsyncData } from './asyncData' @@ -45,7 +46,7 @@ export function useFetch< arg2?: string ) { const [opts = {}, autoKey] = typeof arg1 === 'string' ? [{}, arg1] : [arg1, arg2] - const _key = opts.key || typeof request === 'string' ? withBase(unref(opts.baseURL) || '', withQuery(request as string, unref(opts.params) || {})) : autoKey + const _key = opts.key || (typeof request === 'string' ? hash(withBase(unref(opts.baseURL) || '', withQuery(request as string, unref(opts.params) || {}))) : autoKey) if (!_key || typeof _key !== 'string') { throw new TypeError('[nuxt] [useFetch] key must be a string: ' + _key) } From 4826776936f82558aca48f03dfd7756ca6405658 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 15 Nov 2022 16:41:47 +0000 Subject: [PATCH 3/3] fix: hash pure array --- packages/nuxt/src/app/composables/fetch.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index 5197460908d..5c17ab5e35f 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -1,7 +1,6 @@ import type { FetchError, FetchOptions } from 'ofetch' import type { TypedInternalResponse, NitroFetchRequest } from 'nitropack' import { computed, unref, Ref, reactive } from 'vue' -import { withBase, withQuery } from 'ufo' import { hash } from 'ohash' import type { AsyncDataOptions, _Transform, KeyOfRes, AsyncData, PickFrom } from './asyncData' import { useAsyncData } from './asyncData' @@ -46,7 +45,7 @@ export function useFetch< arg2?: string ) { const [opts = {}, autoKey] = typeof arg1 === 'string' ? [{}, arg1] : [arg1, arg2] - const _key = opts.key || (typeof request === 'string' ? hash(withBase(unref(opts.baseURL) || '', withQuery(request as string, unref(opts.params) || {}))) : autoKey) + const _key = opts.key || hash([autoKey, unref(opts.baseURL), typeof request === 'string' ? request : '', unref(opts.params)]) if (!_key || typeof _key !== 'string') { throw new TypeError('[nuxt] [useFetch] key must be a string: ' + _key) }