diff --git a/src/app.html b/src/app.html index 967fb380..27d1688f 100644 --- a/src/app.html +++ b/src/app.html @@ -2,6 +2,7 @@ diff --git a/src/service-worker.js b/src/service-worker.js index a73e646d..5d9e0e98 100644 --- a/src/service-worker.js +++ b/src/service-worker.js @@ -10,13 +10,13 @@ const ASSETS = [ ]; self.addEventListener('install', (event) => { - // Create a new cache and add all files to it - async function addFilesToCache() { + // Create a new cache and add offline file to it + async function addFileToCache() { const cache = await caches.open(CACHE); - await cache.addAll(ASSETS); + await cache.add('/offline.html'); } - event.waitUntil(addFilesToCache()); + event.waitUntil(addFileToCache()); }); self.addEventListener('activate', (event) => { @@ -30,6 +30,18 @@ self.addEventListener('activate', (event) => { event.waitUntil(deleteOldCaches()); }); +self.addEventListener('message', async (event) => { + if (event.data === 'CACHE_ASSETS') { + // Create a new cache and add all files to it + const cache = await caches.open(CACHE); + const cached = await cache.match('/cached.txt'); + + if (cached) return; + + event.waitUntil(await cache.addAll(ASSETS)); + } +}); + self.addEventListener('fetch', (event) => { // ignore POST requests etc if (event.request.method !== 'GET') return; diff --git a/static/cached.txt b/static/cached.txt new file mode 100644 index 00000000..e69de29b