diff --git a/sw.js b/sw.js index 8d172d68bc..be8abca044 100644 --- a/sw.js +++ b/sw.js @@ -9,15 +9,12 @@ const CACHE = "pwabuilder-precache"; const precacheFiles = [ /* Add an array of files to precache for your app */ - "./index.html", - // Keep in sync with the query-param URL used in index.html. - "./css/activities.css?v=fixed" + "./index.html" ]; self.addEventListener("install", function (event) { // eslint-disable-next-line no-console console.log("[PWA Builder] Install Event processing"); - // eslint-disable-next-line no-console console.log("[PWA Builder] Skip waiting on install"); self.skipWaiting(); @@ -35,51 +32,9 @@ self.addEventListener("install", function (event) { self.addEventListener("activate", function (event) { // eslint-disable-next-line no-console console.log("[PWA Builder] Claiming clients for current page"); - - // Cleanup: remove any previously-cached non-static GET responses. - // This prevents serving stale / user-specific / poisoned cache entries - // that older SW versions may have cached. - event.waitUntil( - (async () => { - await self.clients.claim(); - - const cache = await caches.open(CACHE); - const keys = await cache.keys(); - const keepUrls = new Set(precacheFiles.map(path => new URL(path, self.location).href)); - - for (const request of keys) { - try { - const url = new URL(request.url); - if (keepUrls.has(url.href)) continue; - if (url.origin !== self.location.origin) { - await cache.delete(request); - continue; - } - if (url.search) { - await cache.delete(request); - continue; - } - - const pathname = url.pathname.toLowerCase(); - const isStaticPath = - pathname === "/" || - pathname.endsWith("/index.html") || - /\.(css|js|mjs|json|png|jpe?g|gif|svg|webp|ico|woff2?|ttf|otf|eot|mp3|wav|webm|mp4)$/i.test( - pathname - ); - - if (!isStaticPath) { - await cache.delete(request); - } - } catch { - // If the URL can't be parsed, treat it as unsafe. - await cache.delete(request); - } - } - })() - ); + event.waitUntil(self.clients.claim()); }); - +/* function isPrecachedRequest(request) { try { const url = new URL(request.url); @@ -148,7 +103,7 @@ function shouldCacheResponse(request, response) { // Only cache responses for allowlisted requests (static assets + explicit precache URLs). return isStaticAssetRequest(request) || isPrecachedRequest(request); } - +*/ function updateCache(request, response) { if (response.status === 206) { console.log("Partial response is unsupported for caching."); @@ -179,57 +134,29 @@ function fromCache(request) { self.addEventListener("fetch", function (event) { if (event.request.method !== "GET") return; - // App-shell offline support: serve cached index.html for navigations. - if (isAppShellNavigation(event.request)) { - event.respondWith( - (async () => { - const indexRequest = new Request("./index.html"); - try { - const cached = await fromCache(indexRequest); - // Update the cached app-shell in the background. - event.waitUntil( - fetch(indexRequest).then(function (response) { - if (shouldCacheResponse(indexRequest, response)) { - return updateCache(indexRequest, response.clone()); - } - }) - ); - return cached; - } catch { - // No cached app-shell yet: fall back to network. - return fetch(event.request); - } - })() - ); - return; - } - - // Only use cache-first for explicit precache URLs and allowlisted static assets. - const canUseCache = isPrecachedRequest(event.request) || isStaticAssetRequest(event.request); - if (!canUseCache) { - // Network-only for everything else (prevents caching/serving user-specific responses). - event.respondWith(fetch(event.request)); - return; - } - event.respondWith( fromCache(event.request).then( function (response) { - // Cache hit: return immediately, then update in background. + // The response was found in the cache so we responde + // with it and update the entry + + // This is where we call the server to get the newest + // version of the file to use the next time we show view event.waitUntil( - fetch(event.request).then(function (networkResponse) { - if (shouldCacheResponse(event.request, networkResponse)) { - return updateCache(event.request, networkResponse.clone()); + fetch(event.request).then(function (response) { + if (response.ok) { + return updateCache(event.request, response); } }) ); return response; }, async function () { - // Cache miss: fetch from network and cache if safe. + // The response was not found in the cache so we look + // for it on the server try { const response = await fetch(event.request); - if (shouldCacheResponse(event.request, response)) { + if (response.ok) { event.waitUntil(updateCache(event.request, response.clone())); } return response;