|
| 1 | +const CACHE_NAME = 'python-mini-projects-v1'; |
| 2 | + |
| 3 | +const PRECACHE_ASSETS = [ |
| 4 | + './', |
| 5 | + './index.html', |
| 6 | + './games.html', |
| 7 | + './math.html', |
| 8 | + './utilities.html', |
| 9 | + './faq.html', |
| 10 | + './privacy-policy.html', |
| 11 | + './terms-condition.html', |
| 12 | + './404.html', |
| 13 | + './manifest.json', |
| 14 | + './css/styles.css', |
| 15 | + './assets/favicon.svg', |
| 16 | + './assets/games-bg.webp', |
| 17 | + './assets/math-bg.webp', |
| 18 | + './assets/utilities-bg.webp', |
| 19 | + './js/main.js', |
| 20 | + './js/projects.js', |
| 21 | + './js/playground.js', |
| 22 | + './js/playground-worker.js', |
| 23 | + './js/cm-editor.js', |
| 24 | + './js/audio.js', |
| 25 | + './js/audioManager.js', |
| 26 | + './projects_registry.json' |
| 27 | +]; |
| 28 | + |
| 29 | +const PYODIDE_CDN_PREFIX = 'https://cdn.jsdelivr.net/pyodide/v0.26.2/full/'; |
| 30 | +const PYODIDE_ASSETS = [ |
| 31 | + PYODIDE_CDN_PREFIX + 'pyodide.js', |
| 32 | + PYODIDE_CDN_PREFIX + 'pyodide.asm.js', |
| 33 | + PYODIDE_CDN_PREFIX + 'pyodide.asm.wasm', |
| 34 | + PYODIDE_CDN_PREFIX + 'python_stdlib.zip', |
| 35 | + PYODIDE_CDN_PREFIX + 'pyodide-lock.json' |
| 36 | +]; |
| 37 | + |
| 38 | +// Install Event - Pre-cache core app shell & Pyodide assets |
| 39 | +self.addEventListener('install', (event) => { |
| 40 | + event.waitUntil( |
| 41 | + caches.open(CACHE_NAME).then(async (cache) => { |
| 42 | + await cache.addAll(PRECACHE_ASSETS); |
| 43 | + try { |
| 44 | + await cache.addAll(PYODIDE_ASSETS); |
| 45 | + } catch (err) { |
| 46 | + console.warn('Pyodide caching skipped or partial during SW install:', err); |
| 47 | + } |
| 48 | + }).then(() => self.skipWaiting()) |
| 49 | + ); |
| 50 | +}); |
| 51 | + |
| 52 | +// Activate Event - Clean up old caches |
| 53 | +self.addEventListener('activate', (event) => { |
| 54 | + event.waitUntil( |
| 55 | + caches.keys().then((cacheNames) => { |
| 56 | + return Promise.all( |
| 57 | + cacheNames |
| 58 | + .filter((name) => name !== CACHE_NAME) |
| 59 | + .map((name) => caches.delete(name)) |
| 60 | + ); |
| 61 | + }).then(() => self.clients.claim()) |
| 62 | + ); |
| 63 | +}); |
| 64 | + |
| 65 | +// Fetch Event - Serve from Cache, fallback to Network (and cache dynamically) |
| 66 | +self.addEventListener('fetch', (event) => { |
| 67 | + if (event.request.method !== 'GET') return; |
| 68 | + |
| 69 | + const url = new URL(event.request.url); |
| 70 | + |
| 71 | + // Network-first strategy for navigation requests (HTML pages) |
| 72 | + if (event.request.mode === 'navigate') { |
| 73 | + event.respondWith( |
| 74 | + fetch(event.request) |
| 75 | + .then((networkResponse) => { |
| 76 | + if (networkResponse && networkResponse.status === 200) { |
| 77 | + const responseClone = networkResponse.clone(); |
| 78 | + caches.open(CACHE_NAME).then((cache) => cache.put(event.request, responseClone)); |
| 79 | + } |
| 80 | + return networkResponse; |
| 81 | + }) |
| 82 | + .catch(() => { |
| 83 | + return caches.match(event.request).then((cachedResponse) => { |
| 84 | + return cachedResponse || caches.match('./index.html'); |
| 85 | + }); |
| 86 | + }) |
| 87 | + ); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + // Cache-first strategy for static resources & Pyodide CDN assets |
| 92 | + event.respondWith( |
| 93 | + caches.match(event.request).then((cachedResponse) => { |
| 94 | + if (cachedResponse) { |
| 95 | + return cachedResponse; |
| 96 | + } |
| 97 | + return fetch(event.request).then((networkResponse) => { |
| 98 | + if ( |
| 99 | + networkResponse && |
| 100 | + networkResponse.status === 200 && |
| 101 | + (url.origin === self.location.origin || |
| 102 | + url.hostname === 'cdn.jsdelivr.net' || |
| 103 | + url.hostname === 'cdnjs.cloudflare.com' || |
| 104 | + url.hostname === 'fonts.googleapis.com' || |
| 105 | + url.hostname === 'fonts.gstatic.com') |
| 106 | + ) { |
| 107 | + const responseClone = networkResponse.clone(); |
| 108 | + caches.open(CACHE_NAME).then((cache) => cache.put(event.request, responseClone)); |
| 109 | + } |
| 110 | + return networkResponse; |
| 111 | + }); |
| 112 | + }) |
| 113 | + ); |
| 114 | +}); |
0 commit comments