Created
April 22, 2019 14:03
-
-
Save YSeredich/8f1b90cb5c0f13735e73f4848cb952c0 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 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