diff --git a/pwa/service-worker.js b/pwa/service-worker.js new file mode 100644 index 00000000..2ce1c1d8 --- /dev/null +++ b/pwa/service-worker.js @@ -0,0 +1,34 @@ +'use strict'; + +var cacheVersion = 1; +var currentCache = { + offline: 'offline-cache' + cacheVersion +}; +const offlineUrl = 'offline'; + +this.addEventListener('install', event => { + event.waitUntil( + caches.open(currentCache.offline).then(function (cache) { + return cache.addAll([ + offlineUrl + ]); + }) + ); +}); + +this.addEventListener('fetch', event => { + if (event.request.mode === 'navigate' || (event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html'))) { + event.respondWith( + fetch(event.request.url).catch(error => { + return caches.match(offlineUrl); + }) + ); + } + else { + event.respondWith(caches.match(event.request) + .then(function (response) { + return response || fetch(event.request); + }) + ); + } +}); \ No newline at end of file