// The app calls install once in its classic worker, alongside aviso's handlers.
// Keeping the fallback in the worker avoids caching a login redirect or a page
// rendered for the account that happened to install it.
(function (root) {
"use strict";
const fallback = `
You're offline
You're offline
Connect to the internet, then try again.
Try again`;
let installed = false;
function install({ offlineHTML = fallback } = {}) {
if (installed) throw new Error("pwa: install may only be called once per worker");
if (typeof offlineHTML !== "string" || !offlineHTML.trim()) {
throw new Error("pwa: offlineHTML must be a non-empty public HTML document");
}
installed = true;
const scope = new URL(root.registration.scope);
function inScope(raw) {
const url = new URL(raw);
return url.origin === scope.origin && url.pathname.startsWith(scope.pathname);
}
root.addEventListener("fetch", (event) => {
const request = event.request;
if (request.method !== "GET" || request.mode !== "navigate" || !inScope(request.url)) return;
// Never turn HTTP errors into offline successes, and never persist the
// network response: it may contain a signed-in person's data.
event.respondWith(root.fetch(request, { cache: "no-store" }).catch(() => new Response(offlineHTML, {
status: 503,
headers: {
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "no-store",
"Content-Security-Policy": "default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'",
},
})));
});
root.addEventListener("message", (event) => {
if (event.data?.type !== "rastrillo:pwa:activate" || !event.source?.url || !inScope(event.source.url)) return;
event.waitUntil(root.skipWaiting());
});
// No unconditional skipWaiting or clients.claim: an older tab may still
// have unsaved edits. The app chooses when all tabs can accept an update.
}
root.RastrilloPWA = { install };
})(self);