From 4e3376df93ea7c2b3e184b2670ebe77fe9b15d6a Mon Sep 17 00:00:00 2001 From: Miguel Ribeiro Date: Mon, 18 Mar 2024 23:43:23 +0100 Subject: [PATCH] fix: pwa not loading static files when offline (#241) --- includes/version.php | 2 +- service-worker.js | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/includes/version.php b/includes/version.php index 569ddaa..9b4637d 100644 --- a/includes/version.php +++ b/includes/version.php @@ -1,3 +1,3 @@ \ No newline at end of file diff --git a/service-worker.js b/service-worker.js index f365388..30c545b 100644 --- a/service-worker.js +++ b/service-worker.js @@ -1,7 +1,7 @@ self.addEventListener('install', function(event) { event.waitUntil( caches.open('my-cache').then(function(cache) { - return cache.addAll([ + const urlsToCache = [ '.', 'index.php', 'settings.php', @@ -36,6 +36,8 @@ self.addEventListener('install', function(event) { 'scripts/libs/chart.js', 'scripts/libs/sortable.min.js', 'images/icon/favicon.ico', + 'images/icon/android-chrome-192x192.png', + 'images/screenshots/desktop.png', 'images/wallossolid.png', 'images/wallossolidwhite.png', 'images/siteimages/empty.png', @@ -96,7 +98,15 @@ self.addEventListener('install', function(event) { 'images/uploads/icons/venmo.png', 'images/uploads/icons/verifone.png', 'images/uploads/icons/webmoney.png', - ]); + ]; + + urlsToCache.forEach(function(url) { + fetch(url).then(function(response) { + if (response.ok) { + cache.put(url, response); + } + }); + }); }) ); }); @@ -114,7 +124,7 @@ self.addEventListener('fetch', function(event) { } }).catch(function(error) { // If fetching fails, try to retrieve the response from cache - return caches.match(event.request); + return caches.match(event.request, { ignoreSearch: true }); }) ); }); \ No newline at end of file