-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
31 lines (27 loc) · 782 Bytes
/
sw.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
const cacheName = location.hostname
const addResourcesToCache = async (resources) => {
const cache = await caches.open(cacheName);
await cache.addAll(resources);
};
self.addEventListener('activate', () => {
return self.clients.claim();
});
self.addEventListener('install', (event) => {
event.waitUntil(
addResourcesToCache([
'/',
'/index.html',
'/error.html'
])
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(caches.open(cacheName).then((cache) => {
return cache.match(event.request.url).then((cachedResponse) => {
return cachedResponse || fetch(event.request).then((fetchedResponse) => {
cache.put(event.request.url, fetchedResponse.clone());
return fetchedResponse
})
})
}))
});