From f3d474a9c72dd886f4e3ab68371f44ad5537cbe5 Mon Sep 17 00:00:00 2001 From: Dan Cojocaru Date: Thu, 2 Nov 2023 04:39:46 +0100 Subject: [PATCH] Improve caching --- sw.js | 54 +++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/sw.js b/sw.js index 8e0eec3..6920347 100755 --- a/sw.js +++ b/sw.js @@ -12,52 +12,48 @@ const CACHE_FIRST = [ '/common/back.svg', // Utility JS - '/common/worker.js', - '/common/items.js', '/common/back.js', + '/common/components.js', + '/common/items.js', '/common/tabs.js', '/common/trainId.js', + '/common/worker.js', // Base '/base.css', '/base.dark.css', // Pages - '/index.html', - '/index.js', - '/about.html', '/about.js', - '/train.html', - '/train.js', + '/config-route.html', + '/config-route.js', + '/config-route.css', - '/view-train.html', - '/view-train.js', - '/view-train.css', - '/view-train.dark.css', + '/index.html', + '/index.js', + + '/route.html', + '/route.js', + '/route.css', + '/route.dark.css', '/station.html', '/station.js', + '/train.html', + '/train.js', + '/view-station.html', '/view-station.js', '/view-station.css', '/view-station.dark.css', - '/config-route.html', - '/config-route.js', - '/config-route.css', - - '/route.html', - '/route.js', - '/route.css', - '/route.dark.css', - - // API - API_TRAINS, - API_STATIONS, - // API_ITINERARIES, + '/view-train.html', + '/view-train.js', + '/view-train.css', + '/view-train.dark.css', ]; /** @@ -140,7 +136,9 @@ const putInCache = async (request, response) => { const cacheFirst = async ({ request, preloadResponsePromise, refreshAnyway }) => { // First try to get the resource from the cache - const responseFromCache = await caches.match(request) + const responseFromCache = await caches.match(request, { + ignoreSearch: true, + }) if (responseFromCache) { if (refreshAnyway || (responseFromCache.headers.has('SW-Cached-At') && Date.now() - new Date(responseFromCache.headers.get('SW-Cached-At')).valueOf() > 86400000)) { console.log('[cf] using cache response; refreshing anyway but returning cache', responseFromCache); @@ -232,7 +230,9 @@ const networkFirst = async ({ request, preloadResponsePromise }) => { } // Response from network wasn't ok, try to find in cache - const responseFromCache = await caches.match(request) + const responseFromCache = await caches.match(request, { + ignoreSearch: true, + }) if (responseFromCache) { console.log('[nf] using cache response', responseFromCache) return responseFromCache @@ -255,7 +255,7 @@ self.addEventListener('fetch', (event) => { cacheFirst({ request: event.request, preloadResponsePromise: event.preloadResponse, - refreshAnyway: [API_STATIONS, API_TRAINS].includes(event.request.url.split('?')[0]), + refreshAnyway: [].includes(event.request.url.split('?')[0]), }) ) }