From 5d82bbaa7aab8f8e7c83c73d8c9bda8231fa6495 Mon Sep 17 00:00:00 2001 From: Connor van Spronssen Date: Mon, 6 Jan 2025 21:23:07 +0100 Subject: [PATCH 1/3] fix(nuxt): omit the body from useFetch for GET requests (nuxt#20667) --- packages/nuxt/src/app/composables/fetch.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index 5ce5a87d1f..4d5671f16e 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -27,18 +27,22 @@ interface NitroFetchOptions> = ComputedOptions> -export interface UseFetchOptions< +export type UseFetchOptions< ResT, DataT = ResT, PickKeys extends KeysOf = KeysOf, DefaultT = undefined, R extends NitroFetchRequest = string & {}, M extends AvailableRouterMethod = AvailableRouterMethod, -> extends Omit, 'watch'>, ComputedFetchOptions { - key?: string - $fetch?: typeof globalThis.$fetch - watch?: MultiWatchSources | false -} +> = Omit, 'watch'> & + (M extends 'GET' | 'get' + ? Omit, 'body'> + : ComputedFetchOptions + ) & { + key?: string + $fetch?: typeof globalThis.$fetch + watch?: MultiWatchSources | false + } /** * Fetch data from an API endpoint with an SSR-friendly composable. From f1651149bf05377d8eedcc3c4261fe4334329623 Mon Sep 17 00:00:00 2001 From: Connor van Spronssen Date: Mon, 6 Jan 2025 21:40:39 +0100 Subject: [PATCH 2/3] fix(nuxt): omit the body from useFetch for HEAD requests as well (nuxt#20667) --- 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 4d5671f16e..3701153cf4 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -35,7 +35,7 @@ export type UseFetchOptions< R extends NitroFetchRequest = string & {}, M extends AvailableRouterMethod = AvailableRouterMethod, > = Omit, 'watch'> & - (M extends 'GET' | 'get' + (M extends 'GET' | 'get' | 'HEAD' | 'head' ? Omit, 'body'> : ComputedFetchOptions ) & { From 4d6d5a7fe7464e11417f26959fce123d3ab18a07 Mon Sep 17 00:00:00 2001 From: Connor van Spronssen Date: Mon, 6 Jan 2025 21:47:58 +0100 Subject: [PATCH 3/3] fix(nuxt): add a doc comment describing the omission of the body for GET and HEAD requests (nuxt#20667) --- packages/nuxt/src/app/composables/fetch.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index 3701153cf4..6855b6cdfc 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -27,6 +27,11 @@ interface NitroFetchOptions> = ComputedOptions> +/** + * Options for the useFetch composable. + * For GET and HEAD requests, the body property is automatically + * omitted to align with HTTP/1.1 specifications (RFC 7231). + */ export type UseFetchOptions< ResT, DataT = ResT,