1if( 'function' === typeof importScripts) { 2 importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.5.4/workbox-sw.js'); 3 4workbox.setConfig({ debug: false }); 5 6// Import the ExpirationPlugin module 7const { ExpirationPlugin } = workbox.expiration; 8 9workbox.routing.registerRoute( 10 ({request}) => request.method === 'POST', 11 new workbox.strategies.NetworkOnly() // Use the NetworkOnly strategy 12); 13 14// Cache CSS and JS files 15workbox.routing.registerRoute( 16 // Custom `matchCallback` function to match requests for CSS and JS files 17 //({request}) => request.destination === 'style' || request.destination === 'script', 18 ({request, url}) => (request.destination === 'style' || request.destination === 'script') && !url.pathname.endsWith('/sw.js') && !url.pathname.endsWith('/firebase.js'), 19 new workbox.strategies.CacheFirst({ 20 cacheName: 'assets-cache', 21 plugins: [ 22 new ExpirationPlugin({ 23 maxEntries: 50, 24 maxAgeSeconds: 30 * 24 * 60 * 60, // 30 days 25 }), 26 ], 27 }) 28); 29 30// Cache images in the uploads directory with a shorter expiration time 31workbox.routing.registerRoute( 32 // Custom `matchCallback` function to match requests for images in the uploads directory 33 ({url}) => url.pathname.includes('/uploads/'), 34 new workbox.strategies.CacheFirst({ 35 cacheName: 'uploads-image-cache', 36 plugins: [ 37 new ExpirationPlugin({ 38 maxEntries: 20, 39 maxAgeSeconds: 24 * 60 * 60, // 1 day 40 //maxAgeSeconds: 60, // 1 min 41 }), 42 ], 43 }) 44); 45 46// Cache other images with a longer expiration time 47workbox.routing.registerRoute( 48 // Custom `matchCallback` function to match requests for other images 49 ({event}) => event.request.destination === 'image', 50 new workbox.strategies.CacheFirst({ 51 cacheName: 'image-cache', 52 plugins: [ 53 new ExpirationPlugin({ 54 maxEntries: 50, 55 maxAgeSeconds: 30 * 24 * 60 * 60, // 30 days 56 }), 57 ], 58 }) 59); 60 61// Cache .woff2 font files 62workbox.routing.registerRoute( 63 // Custom `matchCallback` function to match requests for .woff2 font files
64 ({url}) => url.pathname.endsWith('.woff2'), 65 new workbox.strategies.CacheFirst({ 66 cacheName: 'font-cache', 67 plugins: [ 68 new ExpirationPlugin({ 69 maxEntries: 20, 70 maxAgeSeconds: 30 * 24 * 60 * 60, // 30 days 71 }), 72 ], 73 }) 74); 75 76 77self.addEventListener('activate', (event) => { 78 // Array of cache names to delete 79 const cacheWhitelist = ['assets-cache', 'uploads-image-cache', 'image-cache', 'font-cache']; 80 81 event.waitUntil( 82 caches.keys().then((cacheNames) => { 83 return Promise.all( 84 cacheNames.map((cacheName) => { 85 if (!cacheWhitelist.includes(cacheName)) { 86 // Delete the cache 87 return caches.delete(cacheName); 88 } 89 }) 90 ); 91 }) 92 ); 93}); 94 95// // Cache the offline page 96// workbox.precaching.precacheAndRoute([{url: './offline.html', revision: '1'}]); 97 98// // Create a custom route that matches navigation requests 99// workbox.routing.registerRoute( 100// new workbox.routing.NavigationRoute(({event}) => { 101// // Check if the user is offline 102// if (!navigator.onLine) { 103// // Return the cached offline page 104// return caches.match(workbox.precaching.getCacheKeyForURL('./offline.html')); 105// } 106 107// // Use the default network-first strategy for navigation requests 108// return new workbox.strategies.NetworkFirst().handle({event}); 109// }) 110// ); 111 112// self.addEventListener('notificationclick', function(event) { 113// let url = event.notification.click_action; 114// event.notification.close(); 115// event.waitUntil( 116// clients.matchAll({includeUncontrolled: true, type: 'window'}).then(windowClients => { 117// for (let i = 0; i < windowClients.length; i++) { 118// let client = windowClients[i]; 119// if (client.url === url && 'focus' in client) { 120// return client.focus(); 121// } 122// } 123// if (clients.openWindow) { 124// return clients.openWindow(url); 125// } 126// }) 127// ); 128// }); 129 130}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.