-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
44 lines (41 loc) · 1.02 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
const staticCache = 'static-site-v1';
const assets = [
'/',
// '/index.html',
'/scripts/script.js',
'/style.css',
'/favicon.ico',
'/resources/images/header-img.jpg',
'/resources/images/background-medium.jpg',
'https://fonts.googleapis.com/css?family=PT+Sans:400,700',
];
// install sw
self.addEventListener('install', (event) => {
// open cache, create if does not exist and add the items to cache
event.waitUntil(
caches.open(staticCache).then((cache) => {
console.log('caching assets');
cache.addAll(assets);
})
);
});
// activate sw
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) => {
return Promise.all(
keys
.filter((key) => key !== staticCache)
.map((key) => caches.delete(key))
);
})
);
});
// fetch event
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((cacheResponse) => {
return cacheResponse || fetch(event.request);
})
);
});