Skip to content

Commit 73d62d5

Browse files
committed
fix: add force-cache for cachedFetch
1 parent 4ede1f5 commit 73d62d5

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

app/composables/useCachedFetch.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 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

Comments
 (0)