Skip to content

Instantly share code, notes, and snippets.

@melikhov-dev
Created August 30, 2017 06:09
Show Gist options
  • Save melikhov-dev/251d6030d123effd7690ed7b71c1e5e2 to your computer and use it in GitHub Desktop.
Save melikhov-dev/251d6030d123effd7690ed7b71c1e5e2 to your computer and use it in GitHub Desktop.
const cacheName = 'v1';
this.addEventListener('install', function(event) {
event.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll([
'index.html',
'common.css'
].map(u => new Request(u, { credentials: 'include' })))
}).then(()=> console.log('done'))
);
this.skipWaiting();
});
this.addEventListener('fetch', function(event) {
console.log('SW Fetching... ')
let response;
event.respondWith(async function() {
const cachedResponse = await caches.match(event.request);
if (cachedResponse) return cachedResponse;
response = await fetch(event.request);
caches.open(cacheName).then(function(cache) {
cache.put(event.request, response);
});
return response.clone();
}());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment