1// Service Worker for Bekiut Talmud Study App 2// Uses Workbox CDN to avoid build configuration changes 3 4importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.6.0/workbox-sw.js'); 5 6if (workbox) { 7 console.log('Workbox is loaded'); 8 9 // Skip waiting and claim clients immediately 10 workbox.core.skipWaiting(); 11 workbox.core.clientsClaim(); 12 13 // Precache offline fallback 14 workbox.precaching.precacheAndRoute([ 15 { url: '/offline.html', revision: '1' } 16 ]); 17 18 // Cache static assets (JS, CSS, fonts, etc.) - StaleWhileRevalidate 19 workbox.routing.registerRoute( 20 ({ request }) => request.destination === 'script' || 21 request.destination === 'style' || 22 request.destination === 'worker', 23 new workbox.strategies.StaleWhileRevalidate({ 24 cacheName: 'static-assets', 25 plugins: [{ 26 cacheWillUpdate: async ({ response }) => { 27 return response.status === 200 ? response : null; 28 }, 29 }], 30 }) 31 ); 32 33 // Cache same-origin assets under /assets/ - CacheFirst with expiration 34 workbox.routing.registerRoute( 35 ({ url, request }) => url.origin === location.origin && url.pathname.startsWith('/assets/'), 36 new workbox.strategies.CacheFirst({ 37 cacheName: 'app-assets', 38 plugins: [ 39 new workbox.expiration.ExpirationPlugin({ 40 maxEntries: 50, 41 maxAgeSeconds: 30 * 24 * 60 * 60, // 30 days 42 }), 43 ], 44 }) 45 ); 46 47 // Cache Talmud JSON data files - CacheFirst for better performance 48 workbox.routing.registerRoute( 49 ({ url, request }) => url.pathname.match(/\.(json)$/) && url.pathname.includes('talmud-data'), 50 new workbox.strategies.CacheFirst({ 51 cacheName: 'talmud-data', 52 plugins: [ 53 new workbox.expiration.ExpirationPlugin({ 54 maxEntries: 100, 55 maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days 56 }), 57 ], 58 }) 59 ); 60 61 // Cache API calls to Sefaria - NetworkFirst with fallback 62 workbox.routing.registerRoute( 63 ({ url }) => url.origin === 'https://www.sefaria.org', 64 new workbox.strategies.NetworkFirst({ 65 cacheName: 'sefaria-api', 66 networkTimeoutSeconds: 3, 67 plugins: [ 68 new workbox.expiration.ExpirationPlugin({ 69 maxEntries: 50, 70 maxAgeSeconds: 24 * 60 * 60, // 1 day 71 }), 72 ], 73 }) 74 ); 75 76 // Handle navigation requests - NetworkFirst with offline fallback 77 workbox.routing.registerRoute( 78 ({ request }) => request.mode === 'navigate', 79 new workbox.strategies.NetworkFirst({ 80 cacheName: 'navigation', 81 networkTimeoutSeconds: 3, 82 }) 83 ); 84 85 // Catch-all handler for offline fallback 86 workbox.routing.setCatchHandler(({ event }) => { 87 if (event.request.mode === 'navigate') { 88 return caches.match('/offline.html'); 89 } 90 return Response.error(); 91 }); 92 93} else { 94 console.log('Workbox failed to load'); 95}
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.