|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// PyDevices Simulator PWA Service Worker with Cross-Origin Isolation (COI) support. |
| 3 | + |
| 4 | +const CACHE_NAME = "pydevices-simulator-v1"; |
| 5 | + |
| 6 | +const PRECACHE_ASSETS = [ |
| 7 | + "./", |
| 8 | + "./index.html", |
| 9 | + "./simulator.css", |
| 10 | + "./simulator.js", |
| 11 | + "./templates.js", |
| 12 | + "./micropython.json", |
| 13 | + "./pyodide.json", |
| 14 | + "./manifest.json", |
| 15 | + "/assets/img/logo.svg", |
| 16 | + "/assets/img/logo-512.png", |
| 17 | + "/assets/chrome/site.css", |
| 18 | + "/vendor/micropython/micropython.mjs", |
| 19 | + "/vendor/micropython/micropython.wasm", |
| 20 | + "https://cdnjs.cloudflare.com/ajax/libs/lz-string/1.5.0/lz-string.min.js", |
| 21 | + "https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.45.0/min/vs/loader.js", |
| 22 | + "https://pyscript.net/releases/2024.11.1/core.css", |
| 23 | + "https://pyscript.net/releases/2024.11.1/core.js", |
| 24 | +]; |
| 25 | + |
| 26 | +self.addEventListener("install", (event) => { |
| 27 | + event.waitUntil( |
| 28 | + caches.open(CACHE_NAME).then((cache) => { |
| 29 | + return cache.addAll(PRECACHE_ASSETS).catch((err) => { |
| 30 | + console.warn("[Simulator SW] Precache warning:", err); |
| 31 | + }); |
| 32 | + }).then(() => self.skipWaiting()) |
| 33 | + ); |
| 34 | +}); |
| 35 | + |
| 36 | +self.addEventListener("activate", (event) => { |
| 37 | + event.waitUntil( |
| 38 | + caches.keys().then((keys) => { |
| 39 | + return Promise.all( |
| 40 | + keys.map((key) => { |
| 41 | + if (key !== CACHE_NAME) { |
| 42 | + return caches.delete(key); |
| 43 | + } |
| 44 | + }) |
| 45 | + ); |
| 46 | + }).then(() => self.clients.claim()) |
| 47 | + ); |
| 48 | +}); |
| 49 | + |
| 50 | +function withCoiHeaders(response) { |
| 51 | + if (!response || response.status === 0) return response; |
| 52 | + const newHeaders = new Headers(response.headers); |
| 53 | + newHeaders.set("Cross-Origin-Embedder-Policy", "require-corp"); |
| 54 | + newHeaders.set("Cross-Origin-Opener-Policy", "same-origin"); |
| 55 | + newHeaders.set("Cross-Origin-Resource-Policy", "cross-origin"); |
| 56 | + |
| 57 | + return new Response(response.body, { |
| 58 | + status: response.status, |
| 59 | + statusText: response.statusText, |
| 60 | + headers: newHeaders, |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +self.addEventListener("fetch", (event) => { |
| 65 | + const { request } = event; |
| 66 | + if (request.method !== "GET") return; |
| 67 | + |
| 68 | + event.respondWith( |
| 69 | + caches.match(request).then((cached) => { |
| 70 | + if (cached) { |
| 71 | + return withCoiHeaders(cached); |
| 72 | + } |
| 73 | + |
| 74 | + return fetch(request) |
| 75 | + .then((response) => { |
| 76 | + // Cache successful responses for same-origin or known CDNs |
| 77 | + if (response && response.status === 200) { |
| 78 | + const copy = response.clone(); |
| 79 | + caches.open(CACHE_NAME).then((cache) => cache.put(request, copy)); |
| 80 | + } |
| 81 | + return withCoiHeaders(response); |
| 82 | + }) |
| 83 | + .catch(() => { |
| 84 | + // If offline and request is for page, fallback to cached index.html |
| 85 | + if (request.mode === "navigate") { |
| 86 | + return caches.match("./index.html").then(withCoiHeaders); |
| 87 | + } |
| 88 | + throw new Error("Network unavailable"); |
| 89 | + }); |
| 90 | + }) |
| 91 | + ); |
| 92 | +}); |
0 commit comments