-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-cache.ts
More file actions
44 lines (39 loc) · 1.34 KB
/
fetch-cache.ts
File metadata and controls
44 lines (39 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { getLogger } from '@furystack/logging'
import { BundleCache } from './caches/bundle-cache'
import { CacheBase } from './caches/cache-base'
import { workerInjector } from './index'
const primaryCaches: CacheBase[] = [new BundleCache()]
const fallbackCaches: CacheBase[] = [new BundleCache()]
export const fetchCache = async (event: FetchEvent) => {
try {
const availableCaches = primaryCaches.filter((cache) => cache.canBeCached(event.request))
for (const cache of availableCaches) {
const result = await cache.getFromCache(event.request)
if (result) {
return result
}
}
} catch (error) {
getLogger(workerInjector).withScope('FetchCache').error({ message: 'Primary cache error ', data: { error } })
}
try {
const response = await fetch(event.request)
const availableFallbackCaches = fallbackCaches.filter((cache) => cache.canBeCached(event.request))
await Promise.all(availableFallbackCaches.map((c) => c.persist(event.request, response.clone())))
return response
} catch (error) {
if (!navigator.onLine) {
throw error
}
const cached = await caches.match(event.request)
if (cached) {
return cached
}
throw error
}
}
export const clearCache = async () => {
// TODO
// const cache = await caches.open(CACHE_KEY)
// await cache.delete(CACHE_KEY)
}