-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
56 lines (50 loc) · 1.6 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
45
46
47
48
49
50
51
52
53
54
55
56
const filesToCache = [
'/',
'index.html',
'assets/css/style.css',
'assets/js/app.js',
'assets/js/apiTransaction.js',
'assets/favicon/android-chrome-192x192.png',
'assets/favicon/android-chrome-512x512.png',
'assets/favicon/favicon-32x32.png',
'manifest.json'
];
const cacheName = 'imovie-app-v1';
self.addEventListener('install', event => {
console.log('installing service worker and caching static assets...😎😎');
event.waitUntil(
caches.open(cacheName)
.then(cache => {
return cache.addAll(filesToCache);
})
.then(() => {
return self.skipWaiting();
})
);
});
self.addEventListener('activate', event => {
console.log('activating service worker...');
return self.clients.claim();
});
self.addEventListener('fetch', event => {
console.log('Fetch event for ', event.request.url);
event.respondWith(
caches.match(event.request)
.then(response => {
if (response) {
console.log('Found ', event.request.url, ' in cache');
return response;
}
console.log('Network request for ', event.request.url);
return fetch(event.request)
.then(response => {
if (response) {
return response;
}
return fetch(event.request);
});
}).catch(error => {
console.log('You have a ', error);
})
);
});