From d4c05393e540218a122683810300303ee5ade51e Mon Sep 17 00:00:00 2001 From: Til Schmitter Date: Thu, 18 Jun 2026 12:48:18 +0200 Subject: [PATCH] PWA: serviceworker prep / wip --- copyparty/web/manifest.json | 6 ++--- copyparty/web/sw.js | 44 ++++++++++++++----------------------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/copyparty/web/manifest.json b/copyparty/web/manifest.json index c752af05..3202cc40 100644 --- a/copyparty/web/manifest.json +++ b/copyparty/web/manifest.json @@ -19,9 +19,9 @@ "method": "POST", "enctype": "multipart/form-data", "params": { - "title": "name", - "text": "description", - "url": "link", + "title": "share", + "text": "upload", + "url": "/", "files": [ { "name": "files", diff --git a/copyparty/web/sw.js b/copyparty/web/sw.js index 7103a254..b94a5582 100644 --- a/copyparty/web/sw.js +++ b/copyparty/web/sw.js @@ -13,42 +13,30 @@ if ('serviceWorker' in navigator) { event.respondWith( (async () => { const formData = await event.request.formData(); - const link = formData.get("link") || ""; - // Instead of the original URL `/save-bookmark/`, redirect - // the user to a URL returned by the `saveBookmark()` - // function, for example, `/`. - const responseUrl = await saveBookmark(link); + const files = formData.get("files") || ""; + const responseUrl = '/'; // (ToDo: remember last upload dir) + // ToDo: keep file references in clipboard + // -> upload on paste + alert(files) return Response.redirect(responseUrl, 303); })(), ); }); + window.addEventListener('load', async () => { try { - const registration = await navigator.serviceWorker.register( - '/service-worker.js', - { scope: '/' } - ); - console.log('SW registered:', registration.scope); - - // Check for waiting update - if (registration.waiting) { - notifyUserOfUpdate(registration); - } - - // Listen for future updates - registration.addEventListener('updatefound', () => { - const newWorker = registration.installing; - newWorker?.addEventListener('statechange', () => { - if ( - newWorker.state === 'installed' && - navigator.serviceWorker.controller - ) { - notifyUserOfUpdate(registration); - } + 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('SW registration failed:', error); + console.error(`Registration failed with ${error}`); } }); } \ No newline at end of file