Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
Use a custom key for payload (non-GET) HTTP requests to fix hash()
Browse files Browse the repository at this point in the history
…function call that can't recognize `FormData` as valid object.

Disable initial cache for payload (non-GET) methods when using `useFetch()` function.
  • Loading branch information
mrauhu committed Jun 29, 2022
1 parent d4b5ff2 commit 94effb6
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/nuxt/src/app/composables/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface UseFetchOptions<
key?: string
}

const GET = 'GET'

export function useFetch<
ResT = void,
ErrorT = Error,
Expand All @@ -29,7 +31,17 @@ export function useFetch<
if (process.dev && opts.transform && !opts.key) {
console.warn('[nuxt] You should provide a key for `useFetch` when using a custom transform function.')
}
const key = '$f_' + (opts.key || hash([request, { ...opts, transform: null }]))
let key: string
if (!opts.method || opts.method.toUpperCase() === GET) {
key = '$f_' + (opts.key || hash([request, { ...opts, transform: null }]))
} else {
// Using custom key for payload (non-GET) HTTP requests,
// fixes call to a hash function that can't recognize `FormData` as valid object
key = '$f_' + (opts.key || `${opts.method}${request}`)
// Disable initial cache for payload (non-GET) methods
opts.initialCache = false
}

const _request = computed(() => {
let r = request as Ref<FetchRequest> | FetchRequest | (() => FetchRequest)
if (typeof r === 'function') {
Expand Down

0 comments on commit 94effb6

Please sign in to comment.