-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
96 lines (89 loc) · 2.92 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
var CACHE_NAME = 'my-site-cache-v1';
var dataCacheName = 'weatherData-v1';
var urlsToCache = [
'./logo_italika1.png',
'./img/logo_italika.png',
'./index.html',
'./css/estilos_generales_adm.css',
'./css/reset.css',
'./css/menurefacciones.css',
'./css/mano.css',
'./css/tablerodeconciliacion.css',
'./css/jquery-ui.css',
'./css/modalbox.css',
'./css/datatables.min.css',
'./css/font-roboto.css',
'./css/font-awesome.css',
'./js/menu.js',
'./js/datatables.min.js' ,
'./js/jquery-ui.js' ,
'./js/jquery-ventana2.js' ,
'./js/jquery.min.js',
'./img/italika_bandera_mexico.png'
];
self.addEventListener('install', function(event){
console.log('Installing sw...');
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('activate', function(e) {
console.log('[ServiceWorker] Activate');
e.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(keyList.map(function(key) {
if (key !== CACHE_NAME && key !== dataCacheName) {
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);
}
}));
})
);
return self.clients.claim();
});
// self.addEventListener('fetch', function(e) {
// console.log('[ServiceWorker] Fetch', e.request.url);
// e.respondWith(
// caches.match(e.request).then(function(response) {
// return response || fetch(e.request);
// })
// );
// });
self.addEventListener('fetch', function(e) {
console.log('[Service Worker] Fetch', e.request.url);
var dataUrl = 'https://reqres.in/api/users?page=2';
if (e.request.url.indexOf(dataUrl) > -1) {
/*
* When the request URL contains dataUrl, the app is asking for fresh
* weather data. In this case, the service worker always goes to the
* network and then caches the response. This is called the "Cache then
* network" strategy:
* https://jakearchibald.com/2014/offline-cookbook/#cache-then-network
*/
console.log('en sw.fetch() -> ' + dataUrl);
e.respondWith(
caches.open(dataCacheName).then(function(cache) {
return fetch(e.request).then(function(response){
cache.put(e.request.url, response.clone());
console.log('retorna caCHE -> '+ response);
return response;
});
})
);
} else {
/*
* The app is asking for app shell files. In this scenario the app uses the
* "Cache, falling back to the network" offline strategy:
* https://jakearchibald.com/2014/offline-cookbook/#cache-falling-back-to-network
*/
e.respondWith(
caches.match(e.request).then(function(response) {
return response || fetch(e.request);
})
);
}
});