Merge pull request #5 from AverageMarcus/service_worker

Added service worker
This commit is contained in:
Marcus Noble 2017-10-12 18:03:16 +01:00 committed by GitHub
commit 09e95c1b65
2 changed files with 25 additions and 0 deletions

18
src/service-worker.js Normal file
View File

@ -0,0 +1,18 @@
var CACHE = 'v1';
self.addEventListener('fetch', function(event) {
event.respondWith(fetchAndCache(event));
});
function fetchAndCache(event) {
return caches.open(CACHE).then(function (cache) {
return fetch(event.request)
.then(function(response) {
cache.put(event.request, response.clone());
return response;
})
.catch(function() {
return cache.match(event.request);
});
});
}

View File

@ -34,4 +34,11 @@
<link rel="stylesheet" href="/css/main.css?{{moment date 'YYYYMMDDHHMM'}}">
<link rel="alternate" type="application/rss+xml" title="{{ site.title }}" href="{{ site.url }}/feed.xml">
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/service-worker.js', { scope: '/' });
});
}
</script>
</head>