-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
87 lines (62 loc) · 2.01 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
//imports
importScripts('js/sw-utils.js');
const STATIC_CACHE='static-v4';
const DYNAMIC_CACHE='dynamic-v2';
const INMUTABLE_CACHE='inmutable-v1';
const APP_SHELL=[
//'/', lo comentamos por el momento ya que estamos probando en produccion
'index.html',
'css/style.css',
'img/favicon.ico',
'img/avatars/hulk.jpg',
'img/avatars/ironman.jpg',
'img/avatars/spiderman.jpg',
'img/avatars/thor.jpg',
'img/avatars/wolverine.jpg',
'js/app.js',
'js/sw-utils.js'
];
const APP_SHELL_INMUTABLE=[
'https://fonts.googleapis.com/css?family=Quicksand:300,400',
'https://fonts.googleapis.com/css?family=Lato:400,300',
'https://use.fontawesome.com/releases/v5.3.1/css/all.css',
'css/animate.css',
'js/libs/jquery.js'
];
self.addEventListener('install', e=>{
//registramos los archivos en cache al instalar el service worker
const cacheStatic = caches.open(STATIC_CACHE).then(cache => {
cache.addAll(APP_SHELL);
});
const cacheInmutable = caches.open(INMUTABLE_CACHE).then(cache => {
cache.addAll(APP_SHELL_INMUTABLE);
});
e.waitUntil( Promise.all([ cacheStatic, cacheInmutable]));
});
self.addEventListener('activate', e=>{
//eliminamos las antiguas versiones del cache
const respuesta = caches.keys().then( keys =>{
keys.forEach( key=>{
if( key !== STATIC_CACHE && key.includes('static') ){
return caches.delete(key);
}
if( key !== DYNAMIC_CACHE && key.includes('dynamic') ){
return caches.delete(key);
}
});
});
e.waitUntil( respuesta );
});
self.addEventListener('fetch', e=>{
const respuesta= caches.match( e.request ).then(res=>{
if(res){
return res;
}else{
console.log(e.request.url);
return fetch(e.request).then( newRes=>{
return actualizaCacheDinamico( DYNAMIC_CACHE, e.request, newRes);
});
}
});
e.respondWith(respuesta);
});