Files
nanobot/webui/src/main.tsx
T
moranandchengyongru 95287f7435 fix(webui): harden PWA service worker caching and registration
- Exclude /webui/* endpoints from the service worker cache; the
  /webui/bootstrap endpoint issues fresh gateway credentials on every load
  and must never be cached or replayed offline.
- Restrict cache-first handling to hashed /assets/ files (served immutable).
  Un-hashed brand icons and the favicon stay network-first so future icon
  swaps reach installed clients.
- Prune stale hashed assets whenever the app shell refreshes, so old build
  assets cannot accumulate even when sw.js itself is unchanged.
- Serve the cached app shell for offline deep-link navigations.
- Ignore service worker registration failures; add unit tests for the
  service worker and the main-entry registration.
2026-08-11 20:59:06 +08:00

44 lines
1.5 KiB
TypeScript

import ReactDOM from "react-dom/client";
import App from "./App";
import "./globals.css";
import "./i18n";
import { initializeLoopbackRuntimeHost } from "./lib/runtime";
// `crypto.randomUUID` is only defined in secure contexts (HTTPS or localhost).
// LAN access over plain HTTP leaves it undefined, which crashes components that
// generate client-side message IDs. Shim a v4-ish fallback so call sites stay
// uniform across secure and non-secure contexts.
if (typeof globalThis.crypto !== "undefined" && !("randomUUID" in globalThis.crypto)) {
Object.defineProperty(globalThis.crypto, "randomUUID", {
value: () =>
"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
}),
configurable: true,
});
}
const root = document.getElementById("root");
if (!root) throw new Error("root element missing");
initializeLoopbackRuntimeHost();
/* StrictMode disabled: dev double-invokes state updaters; delta accumulation must stay pure — see useNanobotStream. */
ReactDOM.createRoot(root).render(<App />);
if ("serviceWorker" in navigator) {
window.addEventListener("load", () => {
navigator.serviceWorker
.register("/sw.js", {
updateViaCache: "none",
})
.catch(() => {
// Service workers are progressive enhancement; registration failures
// (unsupported proxies, blocked storage) must not break the app.
});
});
}