Created
August 30, 2017 06:09
-
-
Save melikhov-dev/251d6030d123effd7690ed7b71c1e5e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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