-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
188 lines (179 loc) · 6.87 KB
/
service-worker.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
var CACHE_VERSION = 6;
var CURRENT_CACHES = {
prefetch: 'parrots-feast-cache-v' + CACHE_VERSION,
ondemand: 'parrots-feast-cache-ondemand-v' + CACHE_VERSION
};
const urlsToPrefetch = [
'index.html',
'styles.css',
'script.js',
'manifest.json',
'icon-192x192.png',
'apple-touch-icon.png',
'arrow-down.svg',
'arrow-left.svg',
'arrow-right.svg',
'arrow-up.svg',
'background-music-endgame.mp3',
'biscuit1.png',
'biscuit2.png',
'biscuit3.png',
'bolt.svg',
'boxing_bell.wav',
'button_last.png',
'button_top.png',
'clock_tick.wav',
'compress.svg',
'eat_biscuit.wav',
'eat_fruit.mp3',
'eat_seed.wav',
'end_game_background.png',
'end_last_level_background.png',
'end_level_background.png',
'expand.svg',
'favicon-16x16.png',
'favicon-32x32.png',
'favicon.ico',
'fruit1.png',
'fruit2.png',
'fruit3.png',
'gong.wav',
'icon.png',
'parrot.png',
'parrot_flipped.png',
'rocket-disabled.svg',
'rocket.svg',
'scores_background.png',
'seed1.png',
'seed2.png',
'seed3.png',
'start_background.png',
'weight-hanging.svg',
'background-level-arabia.png',
'background-level-armageddon.png',
'background-level-cityrio.png',
'background-level-crystalworld.png',
'background-level-house80.png',
'background-level-japanfantasy.png',
'background-level-jungle.png',
'background-level-pirates.png',
'background-level-savana.png',
'background-level-space.png',
'background-level-jurassic.png',
'background-level-artic.png',
'background-level-superhero.png',
'background-level-catcountry.png',
'background-level-christmas.png',
'background-music-arabia.mp3',
'background-music-armageddon.mp3',
'background-music-cityrio.mp3',
'background-music-crystalworld.mp3',
'background-music-house80.mp3',
'background-music-japanfantasy.mp3',
'background-music-jungle.mp3',
'background-music-pirates.mp3',
'background-music-savana.mp3',
'background-music-space.mp3',
'background-music-jurassic.mp3',
'background-music-artic.mp3',
'background-music-superhero.mp3',
'background-music-catcountry.mp3',
'background-music-christmas.mp3'
];
self.addEventListener('install', function(event) {
var now = Date.now();
console.log('Handling install event. Resources to prefetch:', urlsToPrefetch);
// Elimina le vecchie cache prima di fare il prefetch
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (!Object.values(CURRENT_CACHES).includes(cacheName)) {
console.log('Deleting out of date cache:', cacheName);
return caches.delete(cacheName);
}
})
);
}).then(function() {
// Ora procedi al prefetch delle risorse
return caches.open(CURRENT_CACHES.prefetch).then(function(cache) {
var cachePromises = urlsToPrefetch.map(function(urlToPrefetch) {
var url = new URL(urlToPrefetch, location.href);
url.search += (url.search ? '&' : '?') + 'cache-bust=' + now;
var request = new Request(url, { mode: 'no-cors' });
return fetch(request).then(function(response) {
if (response.status >= 400) {
throw new Error('Request for ' + urlToPrefetch + ' failed with status ' + response.statusText);
} else if (response.status === 206) {
// Se la risposta è parziale, effettua una nuova richiesta per ottenere il file completo
var fullRequest = new Request(url, { headers: { 'Range': 'bytes=0-' } });
return fetch(fullRequest).then(function(fullResponse) {
if (fullResponse.status === 200) {
return cache.put(urlToPrefetch, fullResponse);
} else {
throw new Error('Failed to fetch the complete file for ' + urlToPrefetch);
}
});
} else {
return cache.put(urlToPrefetch, response);
}
}).catch(function(error) {
console.error('Not caching ' + urlToPrefetch + ' due to ' + error);
});
});
return Promise.all(cachePromises).then(function() {
console.log('Pre-fetching complete.');
});
});
}).catch(function(error) {
console.error('Error during the install event:', error);
})
);
});
self.addEventListener('activate', function(event) {
var expectedCacheNames = Object.keys(CURRENT_CACHES).map(function(key) {
return CURRENT_CACHES[key];
});
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (expectedCacheNames.indexOf(cacheName) === -1) {
console.log('Deleting out of date cache:', cacheName);
return caches.delete(cacheName);
}
})
);
})
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
if (response) {
return response; // Risponde con la risorsa cache se disponibile
}
// Se non è nella cache, effettua la richiesta di rete e la mette in cache dinamicamente
return fetch(event.request).then((networkResponse) => {
if (networkResponse && networkResponse.status !== 206) { // Esclude le risposte parziali
return putcache(event.request, networkResponse).then(() => {
return networkResponse;
});
} else {
return networkResponse; // Restituisce comunque la risposta parziale all'utente
}
});
}).catch((error) => {
console.error('Fetching failed:', error);
throw error;
})
);
});
function putcache(request, response) {
if (response.type === "error" || response.type === "opaque" || request.url.startsWith('chrome-extension')) {
return Promise.resolve(); // non mettere in cache errori di rete o richieste da estensioni Chrome
}
return caches
.open(CURRENT_CACHES.ondemand)
.then((cache) => cache.put(request, response.clone()));
}