11import type { CachedFetchResult } from '#shared/utils/fetch-cache-config'
2+ import defu from 'defu'
23
34/**
45 * Get the cachedFetch function from the current request context.
@@ -34,9 +35,12 @@ export function useCachedFetch(): CachedFetchFunction {
3435 return async < T = unknown > (
3536 url : string ,
3637 options : Parameters < typeof $fetch > [ 1 ] = { } ,
37- _ttl ? : number ,
38+ _ttl : number = FETCH_CACHE_DEFAULT_TTL ,
3839 ) : Promise < CachedFetchResult < T > > => {
39- const data = ( await $fetch < T > ( url , options ) ) as T
40+ const defaultFetchOptions : Parameters < typeof $fetch > [ 1 ] = {
41+ cache : 'force-cache' ,
42+ }
43+ const data = ( await $fetch < T > ( url , defu ( options , defaultFetchOptions ) ) ) as T
4044 return { data, isStale : false , cachedAt : null }
4145 }
4246 }
@@ -55,9 +59,12 @@ export function useCachedFetch(): CachedFetchFunction {
5559 return async < T = unknown > (
5660 url : string ,
5761 options : Parameters < typeof $fetch > [ 1 ] = { } ,
58- _ttl ? : number ,
62+ _ttl : number = FETCH_CACHE_DEFAULT_TTL ,
5963 ) : Promise < CachedFetchResult < T > > => {
60- const data = ( await $fetch < T > ( url , options ) ) as T
64+ const defaultFetchOptions : Parameters < typeof $fetch > [ 1 ] = {
65+ cache : 'force-cache' ,
66+ }
67+ const data = ( await $fetch < T > ( url , defu ( options , defaultFetchOptions ) ) ) as T
6168 return { data, isStale : false , cachedAt : null }
6269 }
6370}
0 commit comments