N
Nuxtβ€’10mo ago
Muhammad Mahmoud

`defu` doesn't override the value if `undefined` is passed

Hey πŸ‘‹ We're using a useFetch wrapper to pass extra headers, etc with each request. We want to cache all data by default using getCachedData and opt-out from this when needed
import type { UseFetchOptions } from 'nuxt/app';

export function useCustomFetch<T>(url: string, options: UseFetchOptions<T> = {}) {
const ourDefaults = {
getCachedData: (key) => nuxtApp.payload.data[key] || nuxtApp.static.data[key],
// Some other defaults
}

const options = defu(opts, ourDefaults)

return useFetch(url, options)
}
import type { UseFetchOptions } from 'nuxt/app';

export function useCustomFetch<T>(url: string, options: UseFetchOptions<T> = {}) {
const ourDefaults = {
getCachedData: (key) => nuxtApp.payload.data[key] || nuxtApp.static.data[key],
// Some other defaults
}

const options = defu(opts, ourDefaults)

return useFetch(url, options)
}
It's working okay but when opting out by passing undefined to getCachedData it doesn't apply it. Is there a way to tell defu that I want to explicitly set the key to undefined or do I have to always pass a function that returns undefined as a value?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server