// π€ NOTE: This file creates a service worker that cross-origin-isolates the page (read more here: https://web.dev/coop-coep/) // π Normally you'd set the COOP and COEP headers on the server (yum), but Github Pages said: "No burgers for you!" π // π So, we're doing a cheeky pizza hack π to make this work without server-side headers. /* π οΈ Edited version of: coi-serviceworker v0.1.6 - Guido Zuidhof, licensed under MIT π οΈ */ // π§ββοΈ We're using wizardry from here: https://github.com/gzuidhof/coi-serviceworker if (typeof window === 'undefined') { // π οΈ Service Worker Time! (No windows allowed in this club) // π Install event: "Let's skip the line and go straight to work!" self.addEventListener("install", () => self.skipWaiting()); // π Activate event: "I claim all clients! Mine! All mine!" (Mwahaha π) self.addEventListener("activate", e => e.waitUntil(self.clients.claim())); // π΄ Handle fetch requests, like a master chef in the kitchen! async function handleFetch(request) { // π§ Special seasoning: If we're caching but the mode's not right, we pass. if (request.cache === "only-if-cached" && request.mode !== "same-origin") { return; } // πΆοΈ For no-cors requests, we're keeping it cool π with 'omit' credentials (Bug workarounds are fun, right? π) if (request.mode === "no-cors") { request = new Request(request.url, { cache: request.cache, credentials: "omit", headers: request.headers, integrity: request.integrity, destination: request.destination, keepalive: request.keepalive, method: request.method, mode: request.mode, redirect: request.redirect, referrer: request.referrer, referrerPolicy: request.referrerPolicy, signal: request.signal, }); } // π Fetching data like a worldwide explorer! π§ let r = await fetch(request).catch(e => console.error(e)); // π If the response status is zero, we return it β probably not what we were hoping for, but hey, it's something π€·ββοΈ if (r.status === 0) { return r; } // π© Magic header time! Setting the Cross-Origin rules like a boss π§ββοΈ const headers = new Headers(r.headers); headers.set("Cross-Origin-Embedder-Policy", "credentialless"); // π‘οΈ Or: 'require-corp', for those fancy users headers.set("Cross-Origin-Opener-Policy", "same-origin"); // 𧱠Keep it locked down and safe. return new Response(r.body, { status: r.status, statusText: r.statusText, headers }); } // ποΈ Fetch event listener: "Don't worry, I've got this!" Handling requests like a pro π― self.addEventListener("fetch", function(e) { e.respondWith(handleFetch(e.request)); // π‘ respondWith must be synchronous, like a well-timed joke! (But it can wait for a promise π) }); } else { // π If we're running in a window (hello, browser!), we register the service worker like a superhero suiting up π¦ΈββοΈ (async function() { // β If we're already isolated, let's not double down on the isolation π if (window.crossOriginIsolated !== false) return; // ποΈ Registering the service worker like we're entering the coolest club in town! let registration = await navigator.serviceWorker.register(window.document.currentScript.src).catch(e => console.error("COOP/COEP Service Worker failed to register:", e)); if (registration) { console.log("COOP/COEP Service Worker registered", registration.scope); // π When the service worker updates, we refresh the page like a fresh cup of coffee β registration.addEventListener("updatefound", () => { console.log("Reloading page to make use of updated COOP/COEP Service Worker."); window.location.reload(); }); // π If the service worker is active but not in control, we give the page a fresh reboot π if (registration.active && !navigator.serviceWorker.controller) { console.log("Reloading page to make use of COOP/COEP Service Worker."); window.location.reload(); } } })(); } // ποΈ Code to clean up: "Time to say goodbye!" Unregister the service worker and take out the trash π§Ή // let registrations = await navigator.serviceWorker.getRegistrations(); // for(let registration of registrations) { // await registration.unregister(); // }