From be2772393bc39804196099f82a3a611df1223746 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 9 Nov 2022 12:26:34 +0100 Subject: [PATCH 1/3] fix(nuxt)!: only add `$f` fetch prefix to auto-keys --- packages/nuxt/src/app/composables/fetch.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index d55d7e98dce..304b25610c8 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -44,14 +44,13 @@ export function useFetch< arg2?: string ) { const [opts = {}, autoKey] = typeof arg1 === 'string' ? [{}, arg1] : [arg1, arg2] - const _key = opts.key || autoKey - if (!_key || typeof _key !== 'string') { - throw new TypeError('[nuxt] [useFetch] key must be a string: ' + _key) + const key = opts.key || (autoKey && typeof autoKey === 'string' ? '$f' + autoKey : undefined) + if (!key || typeof key !== 'string') { + throw new TypeError('[nuxt] [useFetch] key must be a string: ' + key) } if (!request) { throw new Error('[nuxt] [useFetch] request is missing.') } - const key = '$f' + _key const _request = computed(() => { let r = request From 8025e93d23c2d5ccb6b51fb7aaef4e32d75a8f9c Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 9 Nov 2022 14:03:57 +0100 Subject: [PATCH 2/3] refactor: simplify check --- packages/nuxt/src/app/composables/fetch.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index 304b25610c8..59d51141b33 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -44,13 +44,14 @@ export function useFetch< arg2?: string ) { const [opts = {}, autoKey] = typeof arg1 === 'string' ? [{}, arg1] : [arg1, arg2] - const key = opts.key || (autoKey && typeof autoKey === 'string' ? '$f' + autoKey : undefined) - if (!key || typeof key !== 'string') { - throw new TypeError('[nuxt] [useFetch] key must be a string: ' + key) + const _key = opts.key || autoKey + if (!_key || typeof _key !== 'string') { + throw new TypeError('[nuxt] [useFetch] key must be a string: ' + _key) } if (!request) { throw new Error('[nuxt] [useFetch] request is missing.') } + const key = _key === autoKey ? '$f' + _key : _key const _request = computed(() => { let r = request From 535da438e051a9221b1bc839e9476af35cfd454a Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 9 Nov 2022 14:28:36 +0100 Subject: [PATCH 3/3] test: update snapshot --- test/basic.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/basic.test.ts b/test/basic.test.ts index 1a79fea1087..cb91e963803 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -778,7 +778,7 @@ describe.skipIf(process.env.NUXT_TEST_DEV || isWindows)('payload rendering', () it('renders a payload', async () => { const payload = await $fetch('/random/a/_payload.js', { responseType: 'text' }) expect(payload).toMatch( - /export default \{data:\{\$frand_a:\[[^\]]*\]\},prerenderedAt:\d*\}/ + /export default \{data:\{rand_a:\[[^\]]*\]\},prerenderedAt:\d*\}/ ) })