-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
174 lines (154 loc) · 5.58 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* globals self */
/* globals caches */
// version: 0.27
"use strict";
var keepOldCache = false;
const appCacheName = 'kepler-cache-v31';
const deprecatedCaches = [
'kepler-cache',
'kepler-cache-v1',
'kepler-cache-v21',
'kepler-cache-v22',
'kepler-cache-v23',
'kepler-cache-v24',
'kepler-cache-v25',
'kepler-cache-v26',
'kepler-cache-v27',
'kepler-cache-v28',
'kepler-cache-v29',
'kepler-cache-v30',
'the-magic-cache'
];
const staticAssets = [
// # find /.../appdir -type f
'./',
'./index.html',
'./pwa.js',
'./manifest.webmanifest',
'./favicon.png',
// './assets/',
// './assets/css/',
'./assets/css/styles.css',
// './assets/js/',
'./assets/js/backend.js',
'./assets/js/dhc.js',
'./assets/js/diphonicpad.js',
'./assets/js/hancock.js',
'./assets/js/harmonicarium.js',
'./assets/js/hstack.js',
'./assets/js/keymap-preset.js',
'./assets/js/midi-hub.js',
'./assets/js/midi-in.js',
'./assets/js/midi-out.js',
'./assets/js/midi-ports.js',
'./assets/js/midi-wml-synthlist.js',
'./assets/js/midi-wml.js',
'./assets/js/synth-ir-default.js',
'./assets/js/synth.js',
'./assets/js/templates.js',
// './assets/js/lib/',
'./assets/js/lib/web-audio-peak-meter_v2.js',
'./assets/js/lib/qwerty-hancock-dist.js',
'./assets/js/lib/synthlist.js',
// './assets/img/',
'./assets/img/agpl.png',
'./assets/img/n-edx.png',
'./assets/img/logo.png',
// './assets/img/appicons/',
'./assets/img/appicons/favicon-16x16.png',
'./assets/img/appicons/favicon-128x128.png',
'./assets/img/appicons/apple-touch-icon-120x120.png',
'./assets/img/appicons/android-chrome-192x192.png',
'./assets/img/appicons/favicon-196x196.png',
'./assets/img/appicons/apple-touch-icon-152x152.png',
'./assets/img/appicons/apple-touch-icon-114x114.png',
'./assets/img/appicons/apple-touch-icon-76x76.png',
'./assets/img/appicons/favicon-96x96.png',
'./assets/img/appicons/apple-touch-icon-57x57.png',
'./assets/img/appicons/apple-touch-icon-72x72.png',
'./assets/img/appicons/apple-touch-icon-60x60.png',
'./assets/img/appicons/apple-touch-icon-144x144.png',
'./assets/img/appicons/favicon-32x32.png',
// './assets/font/',
'./assets/font/IMfellDWpica.woff2',
'./assets/font/SourceCodePro.woff2',
];
self.addEventListener('message', messageEvent => {
switch (messageEvent.data.action) {
case 'skipWaiting':
self.skipWaiting();
console.log('SKIP WAITING!');
break;
case 'appCacheNameRequest':
messageEvent.source.postMessage({
action: 'setAppCacheName',
value: keepOldCache || appCacheName
});
break;
case 'keepOldCache':
// caches.open(appCacheName).then(cache => cache.add('./sw.js'));
keepOldCache = messageEvent.data.value;
}
});
self.addEventListener('fetch', evt => {
evt.respondWith(
caches
.open(appCacheName)
.then( cache => {
return cache
.match(evt.request)
.then( cachedResponse => {
if ( /manifest.webmanifest$/.test(evt.request.url) ) {
return cachedResponse || fetch(evt.request);
} else {
// console.log(`Fetched from CACHE: ${evt.request.url}`);
return cachedResponse; // || fetch(evt.request); // No fallback on network. Fetch only from cache. The app files are all-in-one thing.
}
});
})
);
});
self.addEventListener('install', evt => {
console.info('New ServiceWorker installing...');
// self.skipWaiting();
evt.waitUntil(
caches
.open(appCacheName)
.then( cache => {
console.info(`New ServiceWorker install - Stored all the static assets to the cache: ${appCacheName}`);
return cache.addAll(staticAssets);
})
.catch( err => {
console.error('New ServiceWorker INSTALL ERROR! – ' + err);
})
);
console.info('New ServiceWorker installed.');
});
self.addEventListener('activate', evt => {
if (keepOldCache) {
console.info('New ServiceWorker activeted (because it could not be avoided), but THE APP HAS NOT BEEN UPDATED because the user disabled automatic upgrades.');
if (keepOldCache !== appCacheName) {
caches.delete(appCacheName);
}
} else {
evt.waitUntil(
caches
.keys()
.then( cacheNamesKeys => cacheNamesKeys.filter(cacheName => deprecatedCaches.includes(cacheName)))
.then( cacheNamesKeys => {
Promise.all(
cacheNamesKeys.map( cacheName => {
// if (deprecatedCaches.includes(cacheName)) {
console.log(`New ServiceWorker is deleting old cache: ${cacheName}`);
return caches.delete(cacheName);
// }
})
);
})
.then( () => {
// clients.claim() // Unuseful (https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#clientsclaim)
console.log(`New ServiceWorker activated. The new cache "${appCacheName}" is ready to handle fetches.`);
})
);
}
});