-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
45 lines (38 loc) · 1 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
const VERSION = 'v1';
self.addEventListener('install', event => {
event.waitUntil(precache());
});
self.addEventListener('fetch', event => {
const request = event.request;
// get
if (request.method !== 'GET') {
return;
}
// buscar en cache
event.respondWith(cachedResponse(request));
// actualizar el cache
event.waitUntil(updateCache(request));
});
async function precache() {
const cache = await caches.open(VERSION);
return cache.addAll([
'/',
'/index.html',
'/assets/index.js',
'/assets/MediaPlayer.js',
'/assets/plugins/AutoPlay.js',
'/assets/plugins/AutoPause.js',
'/assets/index.css',
'/assets/BigBuckBunny.mp4',
]);
}
async function cachedResponse(request) {
const cache = await caches.open(VERSION);
const response = await cache.match(request);
return response || fetch(request);
}
async function updateCache(request) {
const cache = await caches.open(VERSION);
const response = await fetch(request);
return cache.put(request, response);
}