Skip to content

Instantly share code, notes, and snippets.

@sintaxi
Created October 5, 2018 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sintaxi/4f0c89f61e184510d0286cec311cf107 to your computer and use it in GitHub Desktop.
Save sintaxi/4f0c89f61e184510d0286cec311cf107 to your computer and use it in GitHub Desktop.
this.addEventListener('install', function(event) {
event.waitUntil(
caches.open('v1.1').then(function(cache) {
return cache.addAll([
'/style.css',
'/app.js',
'../views/404.html',
'../views/offline.html'
]);
})
);
});
this.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request).then(function(resp) {
return resp || fetch(event.request).then(function(response) {
if((event.request.url.includes('chrome-extension'))){
//skip any caching
return response;
}
if (['404','0'].includes(response.status)) {
return caches.match('../views/404.html');
}
return caches.open('v1.1').then(function(cache) {
cache.put(event.request.url, response.clone());
return response;
});
});
}).catch(error => {
return caches.match('../views/offline.html');
})
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment