-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.js
116 lines (110 loc) · 5.29 KB
/
service.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
// You can change cache version to disable old cache.
// However, this is not necesary since fetch handler below, updates caches regularily.
const version = "1.0.0";
const cacheName = `patatap-${version}`;
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(cacheName).then(cache => {
return cache.addAll([
'./index.html',
// Here we try to pre-fetch all the files to ensure the app will work offline on first load.
/*
* NOTE: This is not necessary if the app has been loaded while online at least once after service worker install, since cache handler below caches all files anyway.
* You can remove this list but that would require at least two page loads to enable offline mode - once for sw install and once more to fill up the cache through fetch handler.
*/
'./images/cornelius-logo-192x192.png',
'./images/cornelius-logo.png',
'./images/favicon.ico',
'./images/glow.jpg',
'./images/icons/sync.svg',
'./manifest.json',
'./src/gpgpu.js',
'./src/sequencer.js',
'./src/simulation.js',
'./third-party/equalizer.js',
'./third-party/has.js',
'./third-party/sound.js',
'./third-party/three-r90dev.js',
'./third-party/to-half.js',
'./third-party/two-v0.7.0.js',
'./third-party/url.js',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M01_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M02_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M03_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M04_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M05_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M06_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M07_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M08_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M09_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M10_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M11_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M12_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M13_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M14_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M15_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M16_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M17_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M18_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M19_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M20_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M21_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M22_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M23_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M24_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M25_1.mp3',
'//storage.googleapis.com/cdn.patatap.com/mellow-waves/assets/sounds/M26_1.mp3',
])
.then(() => {
self.skipWaiting();
console.log('sw installed');
});
})
);
});
// Implementation of the stale-while-revalidate caching strategy.
// Following fetch handler always returns cached file if available.
// Subsequently, it fetches the requested url and updates the cache if successful.
// This ensures that files are always serverved as fast as possible (cached).
// However, remotely updated files will take two page reloads to reach the client.
self.addEventListener('fetch', (event) => {
// Following serices break when served from local cache.
// Let's serve them from the server always.
if (/fonts.googleapis/.test(event.request.url)) {
event.respondWith(fetch(event.request));
return;
}
if (/google-analytics/.test(event.request.url)) {
event.respondWith(fetch(event.request));
return;
}
if (/googletagmanager/.test(event.request.url)) {
event.respondWith(fetch(event.request));
return;
}
event.respondWith(
caches.open(cacheName)
.then(cache => {
setTimeout(() => {
cache.addAll([event.request]);
});
return cache.match(event.request, {ignoreSearch: event.request.url.indexOf('?') != -1});
})
.then(response => {
if (response && response.redirected) {
return fetch(event.request);
}
if (!response) {
caches.open(cacheName)
.then(cache => {
cache.addAll([event.request.url]);
});
}
return response || fetch(event.request);
}).catch(console.error)
);
});
self.addEventListener('activate', async (event) => {
event.waitUntil(self.clients.claim());
console.log('sw activated');
});