99const CACHE = "pwabuilder-precache" ;
1010const precacheFiles = [
1111 /* Add an array of files to precache for your app */
12- "./index.html" ,
13- // Keep in sync with the query-param URL used in index.html.
14- "./css/activities.css?v=fixed"
12+ "./index.html"
1513] ;
1614
1715self . addEventListener ( "install" , function ( event ) {
1816 // eslint-disable-next-line no-console
1917 console . log ( "[PWA Builder] Install Event processing" ) ;
20-
2118 // eslint-disable-next-line no-console
2219 console . log ( "[PWA Builder] Skip waiting on install" ) ;
2320 self . skipWaiting ( ) ;
@@ -35,51 +32,9 @@ self.addEventListener("install", function (event) {
3532self . addEventListener ( "activate" , function ( event ) {
3633 // eslint-disable-next-line no-console
3734 console . log ( "[PWA Builder] Claiming clients for current page" ) ;
38-
39- // Cleanup: remove any previously-cached non-static GET responses.
40- // This prevents serving stale / user-specific / poisoned cache entries
41- // that older SW versions may have cached.
42- event . waitUntil (
43- ( async ( ) => {
44- await self . clients . claim ( ) ;
45-
46- const cache = await caches . open ( CACHE ) ;
47- const keys = await cache . keys ( ) ;
48- const keepUrls = new Set ( precacheFiles . map ( path => new URL ( path , self . location ) . href ) ) ;
49-
50- for ( const request of keys ) {
51- try {
52- const url = new URL ( request . url ) ;
53- if ( keepUrls . has ( url . href ) ) continue ;
54- if ( url . origin !== self . location . origin ) {
55- await cache . delete ( request ) ;
56- continue ;
57- }
58- if ( url . search ) {
59- await cache . delete ( request ) ;
60- continue ;
61- }
62-
63- const pathname = url . pathname . toLowerCase ( ) ;
64- const isStaticPath =
65- pathname === "/" ||
66- pathname . endsWith ( "/index.html" ) ||
67- / \. ( c s s | j s | m j s | j s o n | p n g | j p e ? g | g i f | s v g | w e b p | i c o | w o f f 2 ? | t t f | o t f | e o t | m p 3 | w a v | w e b m | m p 4 ) $ / i. test (
68- pathname
69- ) ;
70-
71- if ( ! isStaticPath ) {
72- await cache . delete ( request ) ;
73- }
74- } catch {
75- // If the URL can't be parsed, treat it as unsafe.
76- await cache . delete ( request ) ;
77- }
78- }
79- } ) ( )
80- ) ;
35+ event . waitUntil ( self . clients . claim ( ) ) ;
8136} ) ;
82-
37+ /*
8338function isPrecachedRequest(request) {
8439 try {
8540 const url = new URL(request.url);
@@ -148,7 +103,7 @@ function shouldCacheResponse(request, response) {
148103 // Only cache responses for allowlisted requests (static assets + explicit precache URLs).
149104 return isStaticAssetRequest(request) || isPrecachedRequest(request);
150105}
151-
106+ */
152107function updateCache ( request , response ) {
153108 if ( response . status === 206 ) {
154109 console . log ( "Partial response is unsupported for caching." ) ;
@@ -179,57 +134,29 @@ function fromCache(request) {
179134self . addEventListener ( "fetch" , function ( event ) {
180135 if ( event . request . method !== "GET" ) return ;
181136
182- // App-shell offline support: serve cached index.html for navigations.
183- if ( isAppShellNavigation ( event . request ) ) {
184- event . respondWith (
185- ( async ( ) => {
186- const indexRequest = new Request ( "./index.html" ) ;
187- try {
188- const cached = await fromCache ( indexRequest ) ;
189- // Update the cached app-shell in the background.
190- event . waitUntil (
191- fetch ( indexRequest ) . then ( function ( response ) {
192- if ( shouldCacheResponse ( indexRequest , response ) ) {
193- return updateCache ( indexRequest , response . clone ( ) ) ;
194- }
195- } )
196- ) ;
197- return cached ;
198- } catch {
199- // No cached app-shell yet: fall back to network.
200- return fetch ( event . request ) ;
201- }
202- } ) ( )
203- ) ;
204- return ;
205- }
206-
207- // Only use cache-first for explicit precache URLs and allowlisted static assets.
208- const canUseCache = isPrecachedRequest ( event . request ) || isStaticAssetRequest ( event . request ) ;
209- if ( ! canUseCache ) {
210- // Network-only for everything else (prevents caching/serving user-specific responses).
211- event . respondWith ( fetch ( event . request ) ) ;
212- return ;
213- }
214-
215137 event . respondWith (
216138 fromCache ( event . request ) . then (
217139 function ( response ) {
218- // Cache hit: return immediately, then update in background.
140+ // The response was found in the cache so we responde
141+ // with it and update the entry
142+
143+ // This is where we call the server to get the newest
144+ // version of the file to use the next time we show view
219145 event . waitUntil (
220- fetch ( event . request ) . then ( function ( networkResponse ) {
221- if ( shouldCacheResponse ( event . request , networkResponse ) ) {
222- return updateCache ( event . request , networkResponse . clone ( ) ) ;
146+ fetch ( event . request ) . then ( function ( response ) {
147+ if ( response . ok ) {
148+ return updateCache ( event . request , response ) ;
223149 }
224150 } )
225151 ) ;
226152 return response ;
227153 } ,
228154 async function ( ) {
229- // Cache miss: fetch from network and cache if safe.
155+ // The response was not found in the cache so we look
156+ // for it on the server
230157 try {
231158 const response = await fetch ( event . request ) ;
232- if ( shouldCacheResponse ( event . request , response ) ) {
159+ if ( response . ok ) {
233160 event . waitUntil ( updateCache ( event . request , response . clone ( ) ) ) ;
234161 }
235162 return response ;
0 commit comments