Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
refactor: lazy load (sorta) sw updates
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaduck committed Jan 13, 2024
1 parent 1440a6e commit 6265c65
Showing 1 changed file with 35 additions and 33 deletions.
68 changes: 35 additions & 33 deletions src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,6 @@ import {
} from "./data/schemas.ts";
import { btmStairs, stairs } from "./data/stairs.ts";

const updateSW = registerSW({
onRegisteredSW(swUrl, r) {
const intervalMS = 60 * 60 * 1000;

r &&
// eslint-disable-next-line @typescript-eslint/no-misused-promises
setInterval(async () => {
if (r.installing) {
return;
}

if (Object.hasOwn(navigator, "connection") && !navigator.onLine) {
return;
}

const resp = await fetch(swUrl, {
cache: "no-store",
headers: {
cache: "no-store",
"cache-control": "no-cache",
},
});

if (resp.status === 200) {
await r.update();
}
}, intervalMS);
},
});

await updateSW(true);

const storage: Storage = createStorage({
driver: indexedDbDriver({ base: "app:" }),
});
Expand Down Expand Up @@ -638,7 +606,41 @@ async function toggleDarkMode(): Promise<void> {
}
window.toggleDarkMode = toggleDarkMode;

async function startApp() {
async function updateSw(): Promise<void> {
await registerSW({
onRegisteredSW(swUrl, r) {
const intervalMS = 60 * 60 * 1000;

r &&
// eslint-disable-next-line @typescript-eslint/no-misused-promises
setInterval(async (): Promise<void> => {
if (r.installing !== null) {
return;
}

if (Object.hasOwn(navigator, "connection") && !navigator.onLine) {
return;
}

const resp = await fetch(swUrl, {
cache: "no-store",
headers: {
cache: "no-store",
"cache-control": "no-cache",
},
});

if (resp.status === 200) {
await r.update();
}
}, intervalMS);
},
})(true);
}

async function startApp(): Promise<void> {
await updateSw();

lvl(1);
await applySavedProfiles();

Expand Down

0 comments on commit 6265c65

Please sign in to comment.