-
Notifications
You must be signed in to change notification settings - Fork 35
/
swr-service-worker.js
44 lines (32 loc) · 1.3 KB
/
swr-service-worker.js
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
const CACHE_NAME = 'my-csr-app'
const CACHED_URLS = ['/', ...self.__WB_MANIFEST.map(({ url }) => url)]
const MAX_STALE_DURATION = 7 * 24 * 60 * 60
const precacheAssets = async () => {
await caches.delete(CACHE_NAME)
const cache = await caches.open(CACHE_NAME)
await cache.addAll(CACHED_URLS)
}
const staleWhileRevalidate = async request => {
const documentRequest = request.destination === 'document'
if (documentRequest) request = new Request(self.registration.scope)
const cache = await caches.open(CACHE_NAME)
const cachedResponsePromise = await cache.match(request)
const networkResponsePromise = fetch(request, { headers: { 'X-Installed': 'true' } })
if (documentRequest) {
networkResponsePromise.then(response => cache.put(request, response.clone()))
if ((new Date() - new Date(cachedResponsePromise?.headers.get('date'))) / 1000 > MAX_STALE_DURATION) {
return networkResponsePromise
}
return cachedResponsePromise
}
return cachedResponsePromise || networkResponsePromise
}
self.addEventListener('install', event => {
event.waitUntil(precacheAssets())
self.skipWaiting()
})
self.addEventListener('fetch', event => {
if (['document', 'font', 'script', 'style'].includes(event.request.destination)) {
event.respondWith(staleWhileRevalidate(event.request))
}
})