diff --git a/src/api/request-with-parameters.ts b/src/api/request-with-parameters.ts index 1ae4514..4261d17 100644 --- a/src/api/request-with-parameters.ts +++ b/src/api/request-with-parameters.ts @@ -49,7 +49,7 @@ export async function requestWithParameters({ cache: REQUEST_CACHE, canReadCache, canStoreInCache, - } = getCacheSettings(localCache, customHeaders); + } = getCacheSettings(localCache); if (canReadCache && REQUEST_CACHE.has(key)) { return REQUEST_CACHE.get(key) as Promise; @@ -95,17 +95,13 @@ export async function requestWithParameters({ return jsonPromise; } -function getCacheSettings( - localCache: LocalCacheOptions | undefined, - headers: Record -) { - const cacheControl = headers['Cache-Control']; - const canReadCache = localCache - ? localCache.canReadCache - : !cacheControl?.includes('no-cache'); - const canStoreInCache = localCache - ? localCache.canStoreInCache - : !cacheControl?.includes('no-store'); +function getCacheSettings(localCache: LocalCacheOptions | undefined) { + const canReadCache = localCache?.cacheControl?.includes('no-cache') + ? false + : true; + const canStoreInCache = localCache?.cacheControl?.includes('no-store') + ? false + : true; const cache = localCache?.cache || DEFAULT_REQUEST_CACHE; return { diff --git a/src/sources/types.ts b/src/sources/types.ts index af58e81..fc1d3b5 100644 --- a/src/sources/types.ts +++ b/src/sources/types.ts @@ -49,22 +49,26 @@ export type SourceOptionalOptions = { maxLengthURL?: number; /** - * Local cache options. - * * `canReadCache`: If `true`, the source will try to read from the local memory cache. - * * `canStoreInCache`: If `true`, the source will store the response in the local memory cache. - * * `cache`: A map of promises that are used to store the responses. - * - * If not provided, source will try to detect `CacheControl: no-cache or no-store` headers in the response and disable respective caching modes. - * - * By default, local in-memory caching is enabled + * By default, local in-memory caching is enabled. */ localCache?: LocalCacheOptions; }; export type LocalCacheOptions = { - canReadCache?: boolean; - canStoreInCache?: boolean; + /** + * Map that stores requests and their responses. + */ cache?: Map>; + + /** + * Cache control + * * `no-cache`: If present, the source will always fetch from original source. + * * `no-store`: If present, source will not store result in cache (for later reuse). + * + * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#directives + */ + + cacheControl?: ('no-cache' | 'no-store')[]; }; export type SourceOptions = SourceRequiredOptions &