diff --git a/src/index.tsx b/src/index.tsx index c040411..bbebcf6 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -18,6 +18,7 @@ import { StreamProvidersPage } from "pages/providers"; import { defaultRelays } from "const"; import { CatchAllRoutePage } from "pages/catch-all"; import { SnortContext } from "@snort/system-react"; +import { register } from "serviceWorker"; export enum StreamState { Live = "live", @@ -28,6 +29,8 @@ export enum StreamState { export const System = new NostrSystem({}); export const Login = new LoginStore(); +register(); + Object.entries(defaultRelays).forEach((params) => { const [relay, settings] = params; System.ConnectToRelay(relay, settings); @@ -81,4 +84,4 @@ root.render( -); +); \ No newline at end of file diff --git a/src/serviceWorker.ts b/src/serviceWorker.ts new file mode 100644 index 0000000..8205761 --- /dev/null +++ b/src/serviceWorker.ts @@ -0,0 +1,30 @@ +export function register() { + if ("serviceWorker" in navigator) { + window.addEventListener("load", () => { + registerValidSW("/service-worker.js"); + }); + } +} + +async function registerValidSW(swUrl: string) { + try { + const registration = await navigator.serviceWorker.register(swUrl); + registration.onupdatefound = () => { + const installingWorker = registration.installing; + if (installingWorker === null) { + return; + } + installingWorker.onstatechange = () => { + if (installingWorker.state === "installed") { + if (navigator.serviceWorker.controller) { + console.log("Service worker updated, pending reload"); + } else { + console.log("Content is cached for offline use."); + } + } + }; + }; + } catch (e) { + console.error("Error during service worker registration:", e); + } +}