diff --git a/app/web/src/sworker-boot.tsx b/app/web/src/sworker-boot.tsx index 6d0d28dc..518ba6f1 100644 --- a/app/web/src/sworker-boot.tsx +++ b/app/web/src/sworker-boot.tsx @@ -5,232 +5,231 @@ import { isLocalhost } from "./utils/ui/is-localhost"; import { version } from "../timestamp"; const state = { - updating: false, + updating: false, }; export const sworkerRegister = async (react: { root: null | ReactRoot }) => { - if (navigator.serviceWorker) { - if (!isLocalhost()) { - const sw = await registerServiceWorker(); - const cacheCurrentPage = () => { - const swController = navigator.serviceWorker.controller; - if (swController) { - [location.href, "", "/", "/ed", "/ed/_/_", "/login"].forEach( - (url) => { - swController.postMessage({ - type: "add-cache", - url: url, - }); - }, - ); - } - }; - cacheCurrentPage(); + if (navigator.serviceWorker) { + if (!isLocalhost()) { + const sw = await registerServiceWorker(); + const cacheCurrentPage = () => { + const swController = navigator.serviceWorker.controller; + if (swController) { + [location.href, "", "/", "/ed", "/ed/_/_", "/login"].forEach( + (url) => { + swController.postMessage({ + type: "add-cache", + url: url, + }); + } + ); + } + }; + cacheCurrentPage(); - const curver = localStorage.getItem("prasi-version"); - const swc = navigator.serviceWorker.controller; - if (version !== curver && curver && react.root && swc) { - react.root.render( - <> - -
+ +
{ - swc.postMessage({ - type: "force-update", - }); - if (react.root) - react.root.render( - <> - -
-
- Updating App... -
-
- , - ); - }} - > -
- New Version Available. Click to Update -
-
- , - ); - } + "flex justify-center cursor-pointer" + )} + onClick={() => { + sw.unregister().then(() => { + window.location.reload(); + }); + if (react.root) + react.root.render( + <> + +
+
+ Updating App... +
+
+ + ); + }} + > +
+ New Version Available. Click to Update +
+
+ + ); + } - navigator.serviceWorker.addEventListener("message", (e) => { - cacheCurrentPage(); - if (react.root) { - if (e.data.type === "offline") { - w.offline = true; - const click = () => { - if (react.root) react.root.render(); - }; - setTimeout(click, 5000); - react.root.render( - <> - -
{ + cacheCurrentPage(); + if (react.root) { + if (e.data.type === "offline") { + w.offline = true; + const click = () => { + if (react.root) react.root.render(); + }; + setTimeout(click, 5000); + react.root.render( + <> + +
-
- Network Failed -
-
- , - ); - } + "flex justify-center cursor-pointer" + )} + > +
+ Network Failed +
+
+ + ); + } - if (e.data.type === "activated") { - if (e.data.shouldRefresh && sw) { - react.root.render( - <> - -
+ +
-
- Updating App... -
-
- , - ); + "flex justify-center" + )} + > +
+ Updating App... +
+
+ + ); - sw.unregister().then(() => { - window.location.reload(); - }); - } else { - const localVersion = localStorage.getItem("prasi-version"); - if (localVersion !== e.data.version) { - localStorage.setItem("prasi-version", e.data.version); - const click = () => { - if (react.root) react.root.render(); - }; - setTimeout(click, 5000); - react.root.render( - <> - -
{ + window.location.reload(); + }); + } else { + const localVersion = localStorage.getItem("prasi-version"); + if (localVersion !== e.data.version) { + localStorage.setItem("prasi-version", e.data.version); + const click = () => { + if (react.root) react.root.render(); + }; + setTimeout(click, 5000); + react.root.render( + <> + +
-
- Prasi Updated{" "} - {e.data.version} -
-
- , - ); - } - } - } - } - }); - } else { - navigator.serviceWorker.getRegistrations().then(function (registrations) { - for (let registration of registrations) { - registration.unregister(); - } - }); - } - } + "flex justify-center cursor-pointer" + )} + > +
+ Prasi Updated{" "} + {e.data.version} +
+
+ + ); + } + } + } + } + }); + } else { + navigator.serviceWorker.getRegistrations().then(function (registrations) { + for (let registration of registrations) { + registration.unregister(); + } + }); + } + } }; const registerServiceWorker = async () => { - if ("serviceWorker" in navigator) { - try { - return await navigator.serviceWorker.register( - new URL("./sworker.ts", import.meta.url), - { - type: "module", - scope: "/", - }, - ); - } catch (error) { - console.error(`Registration failed with ${error}`); - } - } + if ("serviceWorker" in navigator) { + try { + return await navigator.serviceWorker.register( + new URL("./sworker.ts", import.meta.url), + { + type: "module", + scope: "/", + } + ); + } catch (error) { + console.error(`Registration failed with ${error}`); + } + } }; export const sworkerAddCache = (base: string) => { - if (navigator.serviceWorker) { - if (!isLocalhost()) { - const swc = navigator.serviceWorker.controller; - if (swc) { - [location.href, "", "/", "/ed", "/ed/_/_", "/login"].forEach((url) => { - swc.postMessage({ - type: "add-cache", - url: url, - }); - }); - if (w.prasiApi && w.prasiApi[base] && w.prasiApi[base].apiEntry) { - const routes = Object.entries(w.prasiApi[base].apiEntry).map( - ([k, v]: any) => ({ - url: v.url, - name: k, - }), - ); + if (navigator.serviceWorker) { + if (!isLocalhost()) { + const swc = navigator.serviceWorker.controller; + if (swc) { + [location.href, "", "/", "/ed", "/ed/_/_", "/login"].forEach((url) => { + swc.postMessage({ + type: "add-cache", + url: url, + }); + }); + if (w.prasiApi && w.prasiApi[base] && w.prasiApi[base].apiEntry) { + const routes = Object.entries(w.prasiApi[base].apiEntry).map( + ([k, v]: any) => ({ + url: v.url, + name: k, + }) + ); - swc.postMessage({ - type: "define-route", - routes, - }); - } - } - } - } + swc.postMessage({ + type: "define-route", + routes, + }); + } + } + } + } };