forked from DaggieBlanqx/iKanban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwa-sw.js
78 lines (67 loc) · 2.57 KB
/
pwa-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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//This is the service worker with the Cache-first network
var CACHE = 'pwabuilder-precache2';
var precacheFiles = [
/* Add an array of files to precache for your app */
'./',
'./index.html',
'./manifest.json',
'https://fonts.googleapis.com/icon?family=Material+Icons',
'https://fonts.googleapis.com/css?family=Roboto',
'https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css',
'https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/fonts/roboto/Roboto-Regular.woff2',
'https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/fonts/roboto/Roboto-Regular.woff',
'css/style.css',
'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js',
'js/typeit.js',
'./img/512x512.png',
'./img/favicon.ico',
'./kanban.png',
'./img/laptop_bg.jpg',
'./img/bg1.png',
'./img/demo.gif',
];
//Install stage sets up the cache-array to configure pre-cache content
self.addEventListener('install', function(evt) {
console.log('[PWA Builder] The service worker is being installed.');
evt.waitUntil(precache().then(function() {
console.log('[PWA Builder] Skip waiting on install');
return self.skipWaiting();
}));
});
//allow sw to control of current page
self.addEventListener('activate', function(event) {
console.log('[PWA Builder] Claiming clients for current page');
return self.clients.claim();
});
self.addEventListener('fetch', function(evt) {
console.log('[PWA Builder] The service worker is serving the asset.'+ evt.request.url);
evt.respondWith(fromCache(evt.request).catch(fromServer(evt.request)));
evt.waitUntil(update(evt.request));
});
function precache() {
return caches.open(CACHE).then(function (cache) {
return cache.addAll(precacheFiles);
});
}
function fromCache(request) {
//we pull files from the cache first thing so we can show them fast
return caches.open(CACHE).then(function (cache) {
return cache.match(request).then(function (matching) {
return matching || Promise.reject('no-match');
});
});
}
function update(request) {
//this is where we call the server to get the newest version of the
//file to use the next time we show view
return caches.open(CACHE).then(function (cache) {
return fetch(request).then(function (response) {
return cache.put(request, response);
});
});
}
function fromServer(request){
//this is the fallback if it is not in the cache to go to the server and get it
return fetch(request).then(function(response){ return response});
}