JavaScript
3
Вклад в тег
var version='5.4.3.5';
self.addEventListener('install',function(ev){
ev.waitUntil(caches.open(version).then(function(cache){
if(location.protocol=='http:'||location.protocol=='https:'){
return fetch('/resource.json').then(function(res){
return res.json();
}).then(function(files){
return cache.addAll(files);
});
}
}));
self.skipWaiting();
});
self.addEventListener('activate',function(ev){
ev.waitUntil(caches.keys().then(function(keyList){
return Promise.all(keyList.map(function(key){
if(version!=key){
return caches.delete(key);
}
}));
}));
});
self.addEventListener('fetch',function(ev){
ev.respondWith(caches.match(ev.request).then(function(res){
return res||fetch(ev.request).then(function(res){
var resToCache=res.clone(),
newHeaders=new Headers(resToCache.headers);
newHeaders.append('X-Powered-By','nginx');
var anotherResponse=new Response(resToCache.body,{
status: resToCache.status,
statusText: resToCache.statusText,
headers: newHeaders
});
caches.open(version).then(function(cache){
if(ev.request.method!='POST'&&(new URL(ev.request.url).protocol=='http:'||new URL(ev.request.url).protocol=='https:')){
cache.put(ev.request,anotherResponse);
}
});
return res;
}).catch(function(){
return caches.match('/offline.html');
});
}));
});