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

@ -21,15 +21,14 @@ export const sworkerRegister = async (react: { root: null | ReactRoot }) => {
type: "add-cache", type: "add-cache",
url: url, url: url,
}); });
}, }
); );
} }
}; };
cacheCurrentPage(); cacheCurrentPage();
const curver = localStorage.getItem("prasi-version"); const curver = localStorage.getItem("prasi-version");
const swc = navigator.serviceWorker.controller; if (version !== curver && curver && react.root && sw) {
if (version !== curver && curver && react.root && swc) {
react.root.render( react.root.render(
<> <>
<Root /> <Root />
@ -42,11 +41,11 @@ export const sworkerRegister = async (react: { root: null | ReactRoot }) => {
right: 0px; right: 0px;
z-index: 999; z-index: 999;
`, `,
"flex justify-center cursor-pointer", "flex justify-center cursor-pointer"
)} )}
onClick={() => { onClick={() => {
swc.postMessage({ sw.unregister().then(() => {
type: "force-update", window.location.reload();
}); });
if (react.root) if (react.root)
react.root.render( react.root.render(
@ -61,14 +60,14 @@ export const sworkerRegister = async (react: { root: null | ReactRoot }) => {
right: 0px; right: 0px;
z-index: 999; z-index: 999;
`, `,
"flex justify-center", "flex justify-center"
)} )}
> >
<div className="bg-blue-400 text-white px-4 py-2 rounded-full text-sm"> <div className="bg-blue-400 text-white px-4 py-2 rounded-full text-sm">
Updating App... Updating App...
</div> </div>
</div> </div>
</>, </>
); );
}} }}
> >
@ -76,7 +75,7 @@ export const sworkerRegister = async (react: { root: null | ReactRoot }) => {
New Version Available. Click to Update New Version Available. Click to Update
</div> </div>
</div> </div>
</>, </>
); );
} }
@ -101,7 +100,7 @@ export const sworkerRegister = async (react: { root: null | ReactRoot }) => {
right: 0px; right: 0px;
z-index: 999; z-index: 999;
`, `,
"flex justify-center cursor-pointer", "flex justify-center cursor-pointer"
)} )}
> >
<div <div
@ -111,7 +110,7 @@ export const sworkerRegister = async (react: { root: null | ReactRoot }) => {
Network Failed Network Failed
</div> </div>
</div> </div>
</>, </>
); );
} }
@ -129,14 +128,14 @@ export const sworkerRegister = async (react: { root: null | ReactRoot }) => {
right: 0px; right: 0px;
z-index: 999; z-index: 999;
`, `,
"flex justify-center", "flex justify-center"
)} )}
> >
<div className="bg-blue-400 text-white px-4 py-2 rounded-full text-sm"> <div className="bg-blue-400 text-white px-4 py-2 rounded-full text-sm">
Updating App... Updating App...
</div> </div>
</div> </div>
</>, </>
); );
sw.unregister().then(() => { sw.unregister().then(() => {
@ -162,7 +161,7 @@ export const sworkerRegister = async (react: { root: null | ReactRoot }) => {
right: 0px; right: 0px;
z-index: 999; z-index: 999;
`, `,
"flex justify-center cursor-pointer", "flex justify-center cursor-pointer"
)} )}
> >
<div <div
@ -173,7 +172,7 @@ export const sworkerRegister = async (react: { root: null | ReactRoot }) => {
<span className="opacity-50">{e.data.version}</span> <span className="opacity-50">{e.data.version}</span>
</div> </div>
</div> </div>
</>, </>
); );
} }
} }
@ -198,7 +197,7 @@ const registerServiceWorker = async () => {
{ {
type: "module", type: "module",
scope: "/", scope: "/",
}, }
); );
} catch (error) { } catch (error) {
console.error(`Registration failed with ${error}`); console.error(`Registration failed with ${error}`);
@ -222,7 +221,7 @@ export const sworkerAddCache = (base: string) => {
([k, v]: any) => ({ ([k, v]: any) => ({
url: v.url, url: v.url,
name: k, name: k,
}), })
); );
swc.postMessage({ swc.postMessage({