register service worker
This commit is contained in:
@ -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(
|
||||
<RouterProvider router={router} />
|
||||
</SnortContext.Provider>
|
||||
</React.StrictMode>
|
||||
);
|
||||
);
|
30
src/serviceWorker.ts
Normal file
30
src/serviceWorker.ts
Normal file
@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user