service worker: use injectManifest
All checks were successful
/ depoly (push) Successful in 1m30s

- a RPC framework is added for further use
- fix an error that the service worker is not
    registered until the settings opened
- added theme-color
- settings: added a item to indicate the offline
    availablity
This commit is contained in:
thislight 2024-10-15 20:30:08 +08:00
parent eb461d3708
commit 46e7f1aaea
No known key found for this signature in database
GPG key ID: A50F9451AC56A63E
11 changed files with 341 additions and 19 deletions

35
src/serviceworker/main.ts Normal file
View file

@ -0,0 +1,35 @@
import { cleanupOutdatedCaches, precacheAndRoute } from "workbox-precaching";
import { clientsClaim } from "workbox-core";
import type { AnyCall } from "./services";
function checkServiceWorker(
self: WorkerGlobalScope,
): self is ServiceWorkerGlobalScope {
return !!(self as unknown as ServiceWorkerGlobalScope).registration;
}
if (checkServiceWorker(self)) {
cleanupOutdatedCaches();
precacheAndRoute(self.__WB_MANIFEST, {
cleanURLs: false
});
// auto update
self.skipWaiting();
clientsClaim();
} else {
throw new TypeError("This entry point must be run in a service worker");
}
self.addEventListener("message", (event: MessageEvent<AnyCall>) => {
if (event.data.method === "ping") {
event.source.postMessage({id: event.data.id, jsonrpc: "2.0", result: undefined})
} else {
event.source.postMessage({
error: {
code: -32601,
message: "Method not found"
}
})
}
})