// No reload or permission prompt is implicit: the app knows about unsaved work // and whether the person actually asked to enable notifications. export async function register({ url = "/sw.js", scope, onUpdate = () => {} } = {}) { if (!globalThis.navigator?.serviceWorker) return null; const registration = await navigator.serviceWorker.register(url, { ...(scope ? { scope } : {}), updateViaCache: "none", }); const seen = new WeakSet(); const notify = () => { const worker = registration.waiting; if (worker && !seen.has(worker)) { seen.add(worker); onUpdate(registration); } }; const watch = () => { registration.installing?.addEventListener("statechange", notify); notify(); }; registration.addEventListener("updatefound", watch); watch(); return registration; } // Activation affects the registration shared by all tabs. Call only after the // app has resolved unsaved work across those tabs; this helper never reloads. export function activateUpdate(registration) { if (!registration?.waiting) return false; registration.waiting.postMessage({ type: "rastrillo:pwa:activate" }); return true; }