Skip to content

Commit

Permalink
feat: modernize service worker
Browse files Browse the repository at this point in the history
Yahoo!
  • Loading branch information
lishaduck committed Jul 14, 2024
1 parent 7947a8b commit f5ee2e4
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 124 deletions.
6 changes: 1 addition & 5 deletions apps/gpa-calculator/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ const sheriffOptions = /** @satisfies {SheriffSettings} */ ({
vitest: false,
// astro: false, // THIS DOESN'T DO ANYTHING!
pathsOveriddes: {
tsconfigLocation: [
"./tsconfig.json",
"./tsconfig.sw.json",
"./tsconfig.eslint.json",
],
tsconfigLocation: ["./tsconfig.json", "./tsconfig.eslint.json"],
},
});

Expand Down
3 changes: 1 addition & 2 deletions apps/gpa-calculator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"format": "prettier . --write",
"format:check": "prettier . --check",
"typecheck:extra": "tsc -p tsconfig.eslint.json",
"typecheck:src": "tsc",
"typecheck:sw": "tsc -p tsconfig.sw.json"
"typecheck:src": "tsc"
},
"browserslist": [
"defaults and fully supports es6-module"
Expand Down
34 changes: 0 additions & 34 deletions apps/gpa-calculator/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import "./styles/global.css";
import { registerSW } from "virtual:pwa-register";
import { type Course, newCourse } from "./data/data-types.js";
import { getElementByIdTyped as getElementById } from "./data/utils.js";
import {
Expand Down Expand Up @@ -352,40 +351,7 @@ async function getStorage(): Promise<void> {
}
}

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();
await getStorage();
}

Expand Down
11 changes: 7 additions & 4 deletions apps/gpa-calculator/src/sw.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { clientsClaim } from "workbox-core";
import { cleanupOutdatedCaches, precacheAndRoute } from "workbox-precaching";

declare let self: ServiceWorkerGlobalScope;

await self.skipWaiting();
clientsClaim();

cleanupOutdatedCaches();

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error: __WB_MANIFEST is a placeholder that workbox-precaching will inject at compile time.
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
precacheAndRoute(self.__WB_MANIFEST ?? []);
precacheAndRoute(self.__WB_MANIFEST);
4 changes: 2 additions & 2 deletions apps/gpa-calculator/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"module": "esnext",
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"lib": ["esnext", "dom", "dom.iterable"],
"lib": ["ESNext", "DOM", "DOM.Iterable", "WebWorker"],
"incremental": true,
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json",
"strict": true,
Expand Down Expand Up @@ -36,5 +36,5 @@
"types": ["vite-plugin-pwa/vanillajs", "vite/client"]
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "src/sw.ts"]
"exclude": ["node_modules"]
}
9 changes: 0 additions & 9 deletions apps/gpa-calculator/tsconfig.sw.json

This file was deleted.

6 changes: 1 addition & 5 deletions apps/phs-map/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ const sheriffOptions = /** @satisfies {SheriffSettings} */ ({
vitest: false,
// astro: false, // THIS DOESN'T DO ANYTHING!
pathsOveriddes: {
tsconfigLocation: [
"./tsconfig.json",
"./tsconfig.sw.json",
"./tsconfig.eslint.json",
],
tsconfigLocation: ["./tsconfig.json", "./tsconfig.eslint.json"],
},
});

Expand Down
3 changes: 1 addition & 2 deletions apps/phs-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"format": "prettier . --write",
"format:check": "prettier . --check",
"typecheck:extra": "tsc -p tsconfig.eslint.json",
"typecheck:src": "tsc",
"typecheck:sw": "tsc -p tsconfig.sw.json"
"typecheck:src": "tsc"
},
"browserslist": [
"defaults and fully supports es6-module"
Expand Down
35 changes: 0 additions & 35 deletions apps/phs-map/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import "./styles/style.css";
import * as PF from "pathfinding";
import { createStorage, type Storage } from "unstorage";
import indexedDbDriver from "unstorage/drivers/indexedb";
import { registerSW } from "virtual:pwa-register";
import { fromZodError } from "zod-validation-error";
import { dom, library } from "@fortawesome/fontawesome-svg-core";
import {
Expand Down Expand Up @@ -603,41 +602,7 @@ async function toggleDarkMode(): Promise<void> {
}
window.toggleDarkMode = toggleDarkMode;

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
11 changes: 7 additions & 4 deletions apps/phs-map/src/sw.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { clientsClaim } from "workbox-core";
import { cleanupOutdatedCaches, precacheAndRoute } from "workbox-precaching";

declare let self: ServiceWorkerGlobalScope;

await self.skipWaiting();
clientsClaim();

cleanupOutdatedCaches();

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error: __WB_MANIFEST is a placeholder that workbox-precaching will inject at compile time.
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
precacheAndRoute(self.__WB_MANIFEST ?? []);
precacheAndRoute(self.__WB_MANIFEST);
4 changes: 2 additions & 2 deletions apps/phs-map/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"module": "esnext",
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"lib": ["esnext", "dom", "dom.iterable"],
"lib": ["ESNext", "DOM", "DOM.Iterable", "WebWorker"],
"incremental": true,
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json",
"strict": true,
Expand Down Expand Up @@ -36,5 +36,5 @@
"types": ["vite-plugin-pwa/vanillajs", "vite/client"]
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "src/sw.ts"]
"exclude": ["node_modules"]
}
9 changes: 0 additions & 9 deletions apps/phs-map/tsconfig.sw.json

This file was deleted.

11 changes: 0 additions & 11 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"typecheck": {
"dependsOn": [
"typecheck:src",
"typecheck:sw",
"typecheck:extra"
]
},
Expand All @@ -39,16 +38,6 @@
],
"inputs": [
"src/**",
"!src/sw.ts",
"tsconfig.json"
]
},
"typecheck:sw": {
"outputs": [
"node_modules/.cache/tsbuildinfo.sw.json"
],
"inputs": [
"src/sw.ts",
"tsconfig.json"
]
},
Expand Down

0 comments on commit f5ee2e4

Please sign in to comment.