From e5eafba88570d03746ce2f097946330f8359c7c8 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Tue, 5 Oct 2021 13:38:55 +0200 Subject: [PATCH] Make pwa not cache serviceworker stuff, as well as livereload variations --- src/pwa/service-worker.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/pwa/service-worker.ts b/src/pwa/service-worker.ts index 42c7353..2703c97 100644 --- a/src/pwa/service-worker.ts +++ b/src/pwa/service-worker.ts @@ -1,8 +1,9 @@ const CACHE_NAME = "sharg-static-cache"; -const CACHE_VERSION = "v0.0.1"; +const CACHE_VERSION = "v0.0.2"; const STATIC_CACHE_NAME = CACHE_NAME + "-" + CACHE_VERSION; -const FILES_TO_CACHE = ["/", "/index.html", "/build/bundle.js", "/build/bundle.css", "/favicon.png"]; +const INITIAL_FILES_TO_CACHE = ["/", "/index.html", "/build/bundle.js", "/build/bundle.css", "/favicon.png"]; +const CACHE_DENYLIST = ["/manifest.json", "/service-worker.js", "livereload.js"]; const globalScope = self as WorkerGlobalScope as ServiceWorkerGlobalScope; @@ -12,7 +13,7 @@ globalScope.addEventListener("install", (event) => { event.waitUntil( caches.open(STATIC_CACHE_NAME).then((cache) => { console.debug("[ServiceWorker] Pre-caching offline page"); - return cache.addAll(FILES_TO_CACHE); + return cache.addAll(INITIAL_FILES_TO_CACHE); }) ); }); @@ -37,19 +38,21 @@ globalScope.addEventListener("activate", (evt) => { }); globalScope.addEventListener("fetch", (event) => { - console.debug("[ServiceWorker] Fetch", event.request.url); + const path = new URL(event.request.url).pathname; + console.debug("[ServiceWorker] Fetch", event.request.url, "==", path); function getFromFetch(cache: Cache) { - console.debug("getFromFetch"); return fetch(event.request).then((response) => { - cache.put(event.request.url, response.clone()); + if (!CACHE_DENYLIST.includes(path)) { + cache.put(path, response.clone()); + } return response; }); } async function getFromCache() { const cache = await caches.open(STATIC_CACHE_NAME); - const cached = await cache.match(event.request); + const cached = await cache.match(path); if (cached !== undefined) { return cached; }