From db4063296b83e24a30c49c66bf20f29c4208ab32 Mon Sep 17 00:00:00 2001 From: Lars Date: Sat, 17 Oct 2026 21:51:22 +0200 Subject: [PATCH] Weise dem Web-App-Manifest den richtigen MIME-Typ zu und registriere einen Service Worker, damit Browser und Handy die Installation anbieten. Co-authored-by: Cursor --- frontend/index.html | 2 + frontend/nginx.conf | 10 +++++ frontend/public/manifest.webmanifest | 1 + frontend/public/sw.js | 60 ++++++++++++++++++++++++++++ frontend/src/main.jsx | 10 +++++ 5 files changed, 83 insertions(+) create mode 100644 frontend/public/sw.js diff --git a/frontend/index.html b/frontend/index.html index 15372c8..4b1d1d1 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -4,8 +4,10 @@ + + Tsumugu diff --git a/frontend/nginx.conf b/frontend/nginx.conf index 46fd4f4..20a91fe 100644 --- a/frontend/nginx.conf +++ b/frontend/nginx.conf @@ -36,6 +36,16 @@ server { proxy_read_timeout 30s; } + # .webmanifest ist in den Standard-MIME-Typen dieses Nginx nicht enthalten. + # octet-stream plus nosniff lässt den Browser das Manifest verwerfen, + # dann fehlt das Installationsangebot. + location = /manifest.webmanifest { + types { + application/manifest+json webmanifest; + } + default_type application/manifest+json; + } + location / { try_files $uri $uri/ /index.html; } diff --git a/frontend/public/manifest.webmanifest b/frontend/public/manifest.webmanifest index 8c90a1e..241eff7 100644 --- a/frontend/public/manifest.webmanifest +++ b/frontend/public/manifest.webmanifest @@ -2,6 +2,7 @@ "name": "Tsumugu", "short_name": "Tsumugu", "description": "Unterlagen im Zusammenhang ordnen und wiederfinden.", + "id": "/", "start_url": "/", "scope": "/", "display": "standalone", diff --git a/frontend/public/sw.js b/frontend/public/sw.js new file mode 100644 index 0000000..6262e61 --- /dev/null +++ b/frontend/public/sw.js @@ -0,0 +1,60 @@ +const CACHE = "tsumugu-shell-v1"; + +self.addEventListener("install", (event) => { + event.waitUntil( + caches + .open(CACHE) + .then((cache) => cache.add("/")) + .then(() => self.skipWaiting()), + ); +}); + +self.addEventListener("activate", (event) => { + event.waitUntil( + caches + .keys() + .then((keys) => Promise.all(keys.filter((key) => key !== CACHE).map((key) => caches.delete(key)))) + .then(() => self.clients.claim()), + ); +}); + +self.addEventListener("fetch", (event) => { + const request = event.request; + if (request.method !== "GET" || request.headers.has("range")) return; + + const url = new URL(request.url); + if (url.origin !== self.location.origin) return; + if (url.pathname.startsWith("/api/") || url.pathname.startsWith("/health")) return; + if (url.pathname === "/sw.js" || url.pathname === "/manifest.webmanifest") return; + + if (request.mode === "navigate") { + event.respondWith(networkFirstNavigation(request)); + return; + } + + if (url.pathname.startsWith("/assets/")) { + event.respondWith(cacheFirst(request)); + } +}); + +async function networkFirstNavigation(request) { + const cache = await caches.open(CACHE); + try { + const response = await fetch(request); + if (response.ok && response.type === "basic") await cache.put("/", response.clone()); + return response; + } catch (error) { + const cached = await cache.match("/"); + if (cached) return cached; + throw error; + } +} + +async function cacheFirst(request) { + const cache = await caches.open(CACHE); + const cached = await cache.match(request); + if (cached) return cached; + const response = await fetch(request); + if (response.ok && response.type === "basic") await cache.put(request, response.clone()); + return response; +} diff --git a/frontend/src/main.jsx b/frontend/src/main.jsx index d81b9e6..c81202d 100644 --- a/frontend/src/main.jsx +++ b/frontend/src/main.jsx @@ -11,3 +11,13 @@ createRoot(document.getElementById("root")).render( , ); + +if (import.meta.env.PROD && "serviceWorker" in navigator) { + const register = () => { + navigator.serviceWorker.register("/sw.js").catch((error) => { + console.error("Service Worker nicht registriert", error); + }); + }; + if (document.readyState === "complete") register(); + else window.addEventListener("load", register); +}