Add service worker to allow installing as a PWA

pull/174/head
Candid Dauth 2021-05-29 16:43:46 +02:00
rodzic 12b8e737bd
commit 68b43bec32
3 zmienionych plików z 30 dodań i 0 usunięć

Wyświetl plik

@ -44,6 +44,9 @@ $(document).on("click", "a", function() {
}
});
if ('serviceWorker' in navigator)
navigator.serviceWorker.register('./sw.js');
const queryParams = decodeQueryString(location.search);
const toBoolean = (val: string, def: boolean) => (val == null ? def : val != "0" && val != "false" && val != "no");

Wyświetl plik

@ -0,0 +1,26 @@
// For now we just add offline support for index.html so that FacilMap can be installed as a PWA
const version = "v1";
self.addEventListener('install', (e) => {
e.waitUntil(caches.open(version).then((cache) => cache.addAll(["/"])));
});
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(keyList.map((key) => {
if(key != version)
return caches.delete(key);
}));
})
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(caches.open(version).then((cache) => {
return fetch(event.request).catch(() => {
return cache.match(event.request, { ignoreSearch: true });
});
}));
});

Wyświetl plik

@ -136,6 +136,7 @@ module.exports = (env: any, argv: any): Configuration[] => {
"favicon.ico",
"favicon.svg",
"manifest.json",
"sw.js",
"opensearch.xml"
].map((file) => ({ from: `${__dirname}/static/${file}` }))
}),