-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw-v1.js
47 lines (42 loc) · 1.21 KB
/
sw-v1.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
45
46
47
'use strict';
const CACHE_STATIC = 'static-cache-v6';
function hndlEventInstall(evt) {
/**
* @returns {Promise<void>}
*/
async function cacheStaticFiles() {
const files = [
'./',
'./nanny.pwa.json',
'./icon.png',
'./index.html',
'./sw-v1.js',
];
const cacheStat = await caches.open(CACHE_STATIC);
await Promise.all(
files.map(function (url) {
return cacheStat.add(url).catch(function (reason) {
console.log(`'${url}' failed: ${String(reason)}`);
});
})
);
}
// wait until all static files will be cached
evt.waitUntil(cacheStaticFiles());
}
function hndlEventFetch(evt) {
async function getFromCache() {
// const cache = await self.caches.open(CACHE_STATIC);
// const cachedResponse = await cache.match(evt.request);
// if (cachedResponse) {
// return cachedResponse;
// }
// wait until resource will be fetched from server and stored in cache
const resp = await fetch(evt.request);
// await cache.put(evt.request, resp.clone());
return resp;
}
evt.respondWith(getFromCache());
}
self.addEventListener('install', hndlEventInstall);
self.addEventListener('fetch', hndlEventFetch);