feat(webui): add PWA support for mobile home screen installation

- Add manifest.json with icons for install prompt
- Add service worker with cache-first for static assets
- Register SW in main.tsx
- Link manifest in index.html
This commit is contained in:
zpljd258
2026-08-11 20:59:06 +08:00
committed by chengyongru
parent 7703cd22eb
commit 43ca12960b
7 changed files with 114 additions and 0 deletions
+1
View File
@@ -14,6 +14,7 @@
<link rel="icon" type="image/svg+xml" href="/brand/nanobot_mark.svg" /> <link rel="icon" type="image/svg+xml" href="/brand/nanobot_mark.svg" />
<link rel="alternate icon" type="image/png" sizes="32x32" href="/brand/nanobot_favicon_32.png" /> <link rel="alternate icon" type="image/png" sizes="32x32" href="/brand/nanobot_favicon_32.png" />
<link rel="apple-touch-icon" sizes="180x180" href="/brand/nanobot_apple_touch.png" /> <link rel="apple-touch-icon" sizes="180x180" href="/brand/nanobot_apple_touch.png" />
<link rel="manifest" href="/manifest.json" />
<style> <style>
html, html,
body, body,
Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 133 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 KiB

+33
View File
@@ -0,0 +1,33 @@
{
"name": "nanobot",
"short_name": "nanobot",
"description": "nanobot AI assistant",
"start_url": "/",
"display": "standalone",
"background_color": "#161618",
"theme_color": "#161618",
"orientation": "any",
"icons": [
{
"src": "/brand/nanobot_apple_touch.png",
"sizes": "180x180",
"type": "image/png"
},
{
"src": "/brand/nanobot_icon_192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/brand/nanobot_icon_512.png",
"sizes": "512x512",
"type": "image/png"
},
{
"src": "/brand/nanobot_icon_maskable.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable"
}
]
}
+72
View File
@@ -0,0 +1,72 @@
const CACHE_NAME = "nanobot-static-v1";
const PRECACHE = ["/", "/manifest.json"];
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(PRECACHE))
);
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(
keys
.filter((k) => k !== CACHE_NAME)
.map((k) => caches.delete(k))
)
)
);
self.clients.claim();
});
self.addEventListener("fetch", (event) => {
const { request } = event;
// Only handle same-origin GET requests
if (request.method !== "GET") return;
if (new URL(request.url).origin !== self.location.origin) return;
const url = new URL(request.url);
const path = url.pathname;
// Never cache API, auth, WebSocket, or HMR paths
if (
path.startsWith("/api") ||
path.startsWith("/auth") ||
path.startsWith("/__nanobot")
) {
return;
}
// Static assets: cache-first (immutable by gateway)
if (/\.(js|css|png|webp|ico|svg|woff2?|ttf|eot)$/.test(path)) {
event.respondWith(
caches.match(request).then((cached) => {
if (cached) return cached;
return fetch(request).then((response) => {
if (response.ok) {
const clone = response.clone();
caches.open(CACHE_NAME).then((c) => c.put(request, clone));
}
return response;
});
})
);
return;
}
// Everything else: network-first (index.html, manifest, etc.)
event.respondWith(
fetch(request)
.then((response) => {
if (response.ok) {
const clone = response.clone();
caches.open(CACHE_NAME).then((c) => c.put(request, clone));
}
return response;
})
.catch(() => caches.match(request))
);
});
+8
View File
@@ -28,3 +28,11 @@ initializeLoopbackRuntimeHost();
/* StrictMode disabled: dev double-invokes state updaters; delta accumulation must stay pure — see useNanobotStream. */ /* StrictMode disabled: dev double-invokes state updaters; delta accumulation must stay pure — see useNanobotStream. */
ReactDOM.createRoot(root).render(<App />); ReactDOM.createRoot(root).render(<App />);
if ("serviceWorker" in navigator) {
window.addEventListener("load", () => {
navigator.serviceWorker.register("/sw.js", {
updateViaCache: "none",
});
});
}