Skip to content

Commit

Permalink
feat(fetch): add update_cache to CacheStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Jan 29, 2023
1 parent 2b3c2e0 commit e1406b1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 8 additions & 0 deletions core/fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ async function _handleCacheStrategy(options: Required<FetchOptions>): Promise<Re
}
}

case 'update_cache': {
const networkResponse = await _handleRemoveDuplicate(options);
if (networkResponse.ok) {
cacheStorage.put(request, networkResponse.clone());
}
return networkResponse;
}

case 'stale_while_revalidate': {
const cachedResponse = await cacheStorage.match(request);
const fetchedResponsePromise = _handleRemoveDuplicate(options).then((networkResponse) => {
Expand Down
10 changes: 9 additions & 1 deletion core/fetch/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type {Methods, QueryParameters} from '@alwatr/type';

export type CacheStrategy = 'network_only' | 'network_first' | 'cache_only' | 'cache_first' | 'stale_while_revalidate';
export type CacheStrategy =
| 'network_only'
| 'network_first'
| 'cache_only'
| 'cache_first'
| 'update_cache'
| 'stale_while_revalidate';

export type CacheDuplicate = 'never' | 'always' | 'until_load' | 'auto';

export interface FetchOptions extends RequestInit {
Expand Down Expand Up @@ -59,6 +66,7 @@ export interface FetchOptions extends RequestInit {
* - `network_first`: Network first, falling back to cache.
* - `cache_only`: Cache only without any network request.
* - `cache_first`: Cache first, falling back to network.
* - `update_cache`: Like `network_only` but with update cache.
* - `stale_while_revalidate`: Fastest strategy, Use cached first but always request network to update the cache.
*
* @default 'network_only'
Expand Down

0 comments on commit e1406b1

Please sign in to comment.