This commit is contained in:
Rizky 2024-01-23 12:57:23 +07:00
parent 4868fa7a39
commit 02193476f7
1 changed files with 189 additions and 190 deletions

View File

@ -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(
<>
<Root />
<div
className={cx(
css`
const curver = localStorage.getItem("prasi-version");
if (version !== curver && curver && react.root && sw) {
react.root.render(
<>
<Root />
<div
className={cx(
css`
position: fixed;
bottom: 20px;
left: 0px;
right: 0px;
z-index: 999;
`,
"flex justify-center cursor-pointer",
)}
onClick={() => {
swc.postMessage({
type: "force-update",
});
if (react.root)
react.root.render(
<>
<Root />
<div
className={cx(
css`
position: fixed;
bottom: 20px;
left: 0px;
right: 0px;
z-index: 999;
`,
"flex justify-center",
)}
>
<div className="bg-blue-400 text-white px-4 py-2 rounded-full text-sm">
Updating App...
</div>
</div>
</>,
);
}}
>
<div className="bg-orange-600 text-white px-4 py-2 rounded-full text-sm select-none">
New Version Available. Click to Update
</div>
</div>
</>,
);
}
"flex justify-center cursor-pointer"
)}
onClick={() => {
sw.unregister().then(() => {
window.location.reload();
});
if (react.root)
react.root.render(
<>
<Root />
<div
className={cx(
css`
position: fixed;
bottom: 20px;
left: 0px;
right: 0px;
z-index: 999;
`,
"flex justify-center"
)}
>
<div className="bg-blue-400 text-white px-4 py-2 rounded-full text-sm">
Updating App...
</div>
</div>
</>
);
}}
>
<div className="bg-orange-600 text-white px-4 py-2 rounded-full text-sm select-none">
New Version Available. Click to Update
</div>
</div>
</>
);
}
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(<Root />);
};
setTimeout(click, 5000);
react.root.render(
<>
<Root />
<div
className={cx(
css`
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(<Root />);
};
setTimeout(click, 5000);
react.root.render(
<>
<Root />
<div
className={cx(
css`
position: fixed;
bottom: 20px;
left: 0px;
right: 0px;
z-index: 999;
`,
"flex justify-center cursor-pointer",
)}
>
<div
className="bg-orange-500 text-white px-4 py-2 rounded-full text-sm"
onClick={click}
>
Network Failed
</div>
</div>
</>,
);
}
"flex justify-center cursor-pointer"
)}
>
<div
className="bg-orange-500 text-white px-4 py-2 rounded-full text-sm"
onClick={click}
>
Network Failed
</div>
</div>
</>
);
}
if (e.data.type === "activated") {
if (e.data.shouldRefresh && sw) {
react.root.render(
<>
<Root />
<div
className={cx(
css`
if (e.data.type === "activated") {
if (e.data.shouldRefresh && sw) {
react.root.render(
<>
<Root />
<div
className={cx(
css`
position: fixed;
bottom: 20px;
left: 0px;
right: 0px;
z-index: 999;
`,
"flex justify-center",
)}
>
<div className="bg-blue-400 text-white px-4 py-2 rounded-full text-sm">
Updating App...
</div>
</div>
</>,
);
"flex justify-center"
)}
>
<div className="bg-blue-400 text-white px-4 py-2 rounded-full text-sm">
Updating App...
</div>
</div>
</>
);
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(<Root />);
};
setTimeout(click, 5000);
react.root.render(
<>
<Root />
<div
className={cx(
css`
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(<Root />);
};
setTimeout(click, 5000);
react.root.render(
<>
<Root />
<div
className={cx(
css`
position: fixed;
bottom: 20px;
left: 0px;
right: 0px;
z-index: 999;
`,
"flex justify-center cursor-pointer",
)}
>
<div
className="bg-green-600 text-white px-4 py-2 rounded-full text-sm"
onClick={click}
>
Prasi Updated{" "}
<span className="opacity-50">{e.data.version}</span>
</div>
</div>
</>,
);
}
}
}
}
});
} else {
navigator.serviceWorker.getRegistrations().then(function (registrations) {
for (let registration of registrations) {
registration.unregister();
}
});
}
}
"flex justify-center cursor-pointer"
)}
>
<div
className="bg-green-600 text-white px-4 py-2 rounded-full text-sm"
onClick={click}
>
Prasi Updated{" "}
<span className="opacity-50">{e.data.version}</span>
</div>
</div>
</>
);
}
}
}
}
});
} 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,
});
}
}
}
}
};