Skip to content

Instantly share code, notes, and snippets.

@YSeredich
Created April 22, 2019 14:03
Show Gist options
  • Save YSeredich/8f1b90cb5c0f13735e73f4848cb952c0 to your computer and use it in GitHub Desktop.
Save YSeredich/8f1b90cb5c0f13735e73f4848cb952c0 to your computer and use it in GitHub Desktop.
const CACHE = 'v1';
self.addEventListener('install', function (event) {
console.log('The service worker is being installed.');
event.waitUntil(precache());
});
self.addEventListener('fetch', function (event) {
console.log('The service worker is serving the assets.');
event.respondWith(fromProxy(event.request));
});
function precache() {
return caches.open(CACHE).then(function (cache) {
return cache.addAll([
'/',
'/index.html',
'/assets/script.js',
'/assets/style.css',
'/data/conf.json',
]);
});
}
function fromProxy(request) {
return caches.open(CACHE).then(function (cache) {
return cache.match(request).then(function (matching) {
return matching || fetch(request);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment