diff --git a/client/README.MD b/client/README.MD index 4b27fbd26..855fd6c27 100644 --- a/client/README.MD +++ b/client/README.MD @@ -1,3 +1,7 @@ # Spoolman UI A simple [refine](https://github.com/refinedev/refine) based UI for manipulating the data in the Spoolman database. + +## Local Review Cache Reset + +Set `VITE_BYPASS_LOOPBACK_PWA_CACHE=true` when you want loopback URLs such as `localhost`, `127.0.0.1`, or `::1` to clear existing service workers and browser caches before the app boots. This is intended for local review and debugging when stale PWA assets are masking the current frontend bundle. diff --git a/client/src/index.tsx b/client/src/index.tsx index 75e4dcd06..056aade8e 100644 --- a/client/src/index.tsx +++ b/client/src/index.tsx @@ -5,6 +5,32 @@ import { createRoot } from "react-dom/client"; import App from "./App"; import "./i18n"; +const shouldBypassLoopbackPwaCache = import.meta.env["VITE_BYPASS_LOOPBACK_PWA_CACHE"] === "true"; +const normalizedHostname = window.location.hostname.replace(/^\[|\]$/g, ""); +const isLoopbackHost = + normalizedHostname === "localhost" || + normalizedHostname === "::1" || + /^127(?:\.\d{1,3}){3}$/.test(normalizedHostname); + +if (shouldBypassLoopbackPwaCache && isLoopbackHost) { + // Opt-in local review builds can clear loopback-host PWA state before boot so stale assets + // from earlier test runs do not mask the current bundle. + if ("serviceWorker" in navigator) { + void navigator.serviceWorker.getRegistrations().then((registrations) => { + registrations.forEach((registration) => { + void registration.unregister(); + }); + }); + } + if ("caches" in window) { + void caches.keys().then((keys) => { + keys.forEach((key) => { + void caches.delete(key); + }); + }); + } +} + const container = document.getElementById("root") as HTMLElement; const root = createRoot(container);