mirror of
https://codeberg.org/ashley/poke.git
synced 2024-11-25 22:58:55 +01:00
Add service worker
This commit is contained in:
parent
ba99f09304
commit
004d6efea1
1 changed files with 34 additions and 0 deletions
34
pwa/service-worker.js
Normal file
34
pwa/service-worker.js
Normal file
|
@ -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);
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
Loading…
Reference in a new issue