diff --git a/copyparty/web/sw.js b/copyparty/web/sw.js index d6b30825..5f8b48f3 100644 --- a/copyparty/web/sw.js +++ b/copyparty/web/sw.js @@ -4,7 +4,7 @@ if ('serviceWorker' in navigator) { self.addEventListener("fetch", (event) => { // Regular requests not related to Web Share Target. - if (event.request.method !== "POST" || !event.request.enctype.has("share-target")) { + if (event.request.method !== "POST" || !event.request.action.has("share-target")) { event.respondWith(fetch(event.request)); return; } @@ -23,21 +23,4 @@ if ('serviceWorker' in navigator) { })(), ); }); - - window.addEventListener('load', async () => { - try { - const registration = await navigator.serviceWorker.register("/.cpr/w/sw.js", { - scope: "/", - }); - if (registration.installing) { - console.log("Service worker installing"); - } else if (registration.waiting) { - console.log("Service worker installed"); - } else if (registration.active) { - console.log("Service worker active"); - } - } catch (error) { - console.error(`Registration failed with ${error}`); - } - }); } \ No newline at end of file diff --git a/copyparty/web/util.js b/copyparty/web/util.js index f64381d5..0ef81497 100644 --- a/copyparty/web/util.js +++ b/copyparty/web/util.js @@ -2421,4 +2421,25 @@ function xhrchk(xhr, prefix, e404, lvl, tag) { return fun(0, prefix + xhr.status + ": " + errtxt, tag); } +// register service worker +// ToDo: set http header: Service-Worker-Allowed: / +// otherwise it will fail to register +// https://stackoverflow.com/questions/49084718/how-exactly-add-service-worker-allowed-to-register-service-worker-scope-in-upp +window.addEventListener('load', async () => { + try { + const registration = await navigator.serviceWorker.register("/.cpr/w/sw.js", { + scope: "/", + }); + if (registration.installing) { + console.log("Service worker installing"); + } else if (registration.waiting) { + console.log("Service worker installed"); + } else if (registration.active) { + console.log("Service worker active"); + } + } catch (error) { + console.error(`Registration failed with ${error}`); + } +}); + J_UTL = 2;