-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwa.js
104 lines (88 loc) · 3.46 KB
/
pwa.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// version and contentToCache are supplied by IDEController.py during deploy
const cacheName = 'conflictDemo-2021-03-05 12:50:29.707116'; // unique value, generated each deploy
const contentToCache = [
'index.html',
'code.js',
'toolbox/as/dist/asStyle.css',
'nsb/images/ajax-loader.gif',
'nsb/images/72.png',
'nsb/images/512.png',
'nsb/images/192.png',
'nsb/library/appstudioFunctions.js',
'nsb/library/jquery3.js',
'nsb/library/jquery.modal.min.css',
'nsb/library/jquery.modal.min.js',
];
const trace = false;
if (trace) console.log('[pwa.js] Startup', cacheName);
self.addEventListener('activate', ((e) => {
'use strict';
if (trace) console.log('[pwa.js] Clear old caches');
e.waitUntil(
caches.keys().then((keyList) => {
if (trace) console.log('keylist', keyList);
return Promise.all(keyList.map((key) => {
if (trace) console.log(' Key:', key);
if (cacheName.indexOf(key) === -1 && key.substr(0, 'conflictDemo'.length) === 'conflictDemo') {
if (trace) console.log(' Delete:', key);
return caches.delete(key);
}
}));
}),
);
}));
self.addEventListener('install', (e) => {
'use strict';
if (trace) console.log('[pwa.js] Install');
e.waitUntil(
caches.open(cacheName).then((cache) => {
if (trace) console.log('[pwa.js] Caching all: app shell and content');
return cache.addAll(contentToCache);
}),
);
});
self.addEventListener('fetch', (e) => {
'use strict';
if (trace) console.log('[pwa.js] fetch', e.request.url)
// override Chromium bug: https://stackoverflow.com/questions/48463483/what-causes-a-failed-to-execute-fetch-on-serviceworkerglobalscope-only-if
if (e.request.cache === 'only-if-cached' && e.request.mode !== 'same-origin') return;
e.respondWith(
caches.open(cacheName)
.then(cache => cache.match(e.request, { ignoreSearch: true }))
.then(response => response || fetch(e.request)),
);
});
if (typeof self.skipWaiting === 'function') {
if (trace) console.log('[pwa.js] self.skipWaiting() is supported.');
self.addEventListener('install', function(e) {
// See https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-global-scope-skipwaiting
e.waitUntil(self.skipWaiting());
});
} else {
if (trace) console.log('[pwa.js] self.skipWaiting() is not supported.');
}
if (self.clients && (typeof self.clients.claim === 'function')) {
if (trace) console.log('[pwa.js] self.clients.claim() is supported.');
self.addEventListener('activate', function(e) {
// See https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#clients-claim-method
e.waitUntil(self.clients.claim());
});
} else {
if (trace) console.log('[pwa.js] self.clients.claim() is not supported.');
}
/* Add to home screen prompt:
https://developers.google.com/web/fundamentals/app-install-banners/#criteria
- needs an Install button to show
- Should PWA stuff happen for PhoneGap? no, but we can't check for cordova.js
- Any reason to have a switch for PWA?
- What triggers the update? Simple refresh doesn't do it.
https://developers.google.com/web/fundamentals/primers/service-workers/#update-a-service-worker
- Clear cache happens on activate. When does that happen?
https://github.com/deanhume/pwa-update-available
- remove AddToHomeScreen on Chrome
https://developers.google.com/web/updates/2019/05/mini-infobar-update
*/
/*
Get list of current caches:
caches.keys().then((keyList) => {console.log(keyList)})
*/