Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.