This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
executable file
·202 lines (172 loc) · 5.05 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// import IndexedDB
importScripts('src/js/idb.js');
importScripts('src/js/utility.js');
// quando cambi questi valori modificali anche in view/layouts/js_sw.php
var CACHE_STATIC_NAME = 'dali-static-v01';
var CACHE_DYNAMIC_NAME = 'dali-dynamic-v01';
var STATIC_FILES = [
'/',
'index.php',
'offline.php',
];
// Funzione Fix per apache
function cleanResponse(response) {
const clonedResponse = response.clone();
// Not all browsers support the Response.body stream, so fall back to reading
// the entire body into memory as a blob.
const bodyPromise = 'body' in clonedResponse ?
Promise.resolve(clonedResponse.body) :
clonedResponse.blob();
return bodyPromise.then((body) => {
// new Response() is happy when passed either a stream or a Blob.
return new Response(body, {
headers: clonedResponse.headers,
status: clonedResponse.status,
statusText: clonedResponse.statusText,
});
});
}
function trimCache(cacheName, maxItems) {
caches.open(cacheName)
.then(function(cache) {
return cache.keys()
.then(function(keys) {
if (keys.lenght > maxItems) {
cache.delete(keys[0])
.then(trimCache(cacheName, maxItems));
}
});
});
}
self.addEventListener('install', function (event) {
//console.log('[Service Worker] Installing Service worker...', event);
console.log('[Service Worker] Installing Service worker...');
event.waitUntil(
//versioning della cache. Per aggiornare le versioni del software
caches.open(CACHE_STATIC_NAME)
.then(function(cache){
console.log('[Service Worker] Precaching app shell...');
cache.addAll(STATIC_FILES);
})
)
});
self.addEventListener('activate', function (event) {
console.log('[Service Worker] Activating Service worker...');
event.waitUntil(
caches.keys()
.then(function(keyList) {
return Promise.all(keyList.map(function(key){
if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME){
console.log('[Service Worker] deleting cache', key);
return caches.delete(key);
}
}));
})
);
return self.clients.claim();
});
function isInArray(string, array) {
for(var i = 0; i < array.length; i++) {
if (array[i] === string){
return true;
}
}
return false;
}
function getFileExtension(filename) {
return filename.split('.').pop();
}
// restituisco sempre l'originale e non carico da cache
self.addEventListener('fetch', function (event) {
var parser = new URL(event.request.url);
if (getFileExtension(parser.pathname) == 'php'
|| getFileExtension(parser.pathname) == 'css'
){
console.log('[SW Parser] web ',parser.pathname);
event.respondWith(
fetch(event.request)
);
} else if (isInArray(event.request.url, STATIC_FILES)) {
console.log('[SW Parser] static cache ',parser.pathname);
event.respondWith(
fetch(event.request).catch(function(){
return caches.match(event.request);
})
);
} else {
console.log('[SW Parser] dynamic cache ',parser.pathname);
event.respondWith(
caches.match(event.request)
.then(function(response) {
if (response) {
// Inizio Fix per apache
if(response.redirected) {
return cleanResponse(response);
} else {
return response;
}
// END Fix per apache
} else {
return fetch(event.request)
.then(function(res) {
return caches.open(CACHE_DYNAMIC_NAME)
.then(function(cache) {
//trimCache(CACHE_DYNAMIC_NAME, 20);
cache.put(event.request.url, res.clone());
return res;
})
}).
catch(function(err) {
return caches.open(CACHE_STATIC_NAME)
.then(function(cache) {
if (event.request.headers.get('accept').includes('text/html')){
return cache.match('offline.php');
}
})
});
}
})
);
}
});
//listener per le notifiche push
self.addEventListener('push', function(event) {
console.log('[Service Worker] Push Received.');
//console.log(`[Service Worker] Push had this data: "${event.data.text()}"`);
const info = JSON.parse(`${event.data.text()}`);
console.log(`[Service Worker] Push had this data: `+info);
const sendNotification = body => {
return self.registration.showNotification(info.title, {
body: info.body,
icon: info.icon,
badge: info.badge,
vibrate: info.vibrate,
//image: info.image,
//sound: info.sound,
data: info.data,
actions: info.actions,
tag: info.tag,
renotify: true,
data: {
openUrl: info.openUrl,
},
});
};
if (event.data) {
const message = event.data.text();
event.waitUntil(sendNotification(message));
}
});
self.addEventListener('notificationclick', function(event) {
console.log('[SW event on notification click] EVENT', event);
if (typeof event.notification.data !== 'undefined') {
var action = event.notification.data;
}else{
var action = JSON.parse(event.actions);
}
console.log('[SW event on notification click] ACTION', action);
console.log('[SW event on notification click] URL', action.openUrl);
event.notification.close();
event.waitUntil(clients.openWindow(action.openUrl));
}
, false);