diff --git a/frontend/src/map/map.ts b/frontend/src/map/map.ts index 37bcd698..666cec96 100644 --- a/frontend/src/map/map.ts +++ b/frontend/src/map/map.ts @@ -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"); diff --git a/frontend/static/sw.js b/frontend/static/sw.js new file mode 100644 index 00000000..1a4282e4 --- /dev/null +++ b/frontend/static/sw.js @@ -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 }); + }); + })); +}); diff --git a/frontend/webpack.config.ts b/frontend/webpack.config.ts index 31f8f998..a45b1f47 100644 --- a/frontend/webpack.config.ts +++ b/frontend/webpack.config.ts @@ -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}` })) }),