diff --git a/src/app.tsx b/src/app.tsx index f7e813483..5dc59bad8 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -53,14 +53,14 @@ import emojisRoutes from "./views/emojis/routes"; import walletRoutes from "./views/wallet/routes"; import podcastsRoutes from "./views/podcasts/routes"; -const getScrollKey = (location: Location) => location.pathname + location.search + location.hash; +// const getScrollKey = (location: Location) => location.pathname + location.search + location.hash; const RootPage = () => { useSetColorMode(); return ( - + ); @@ -69,7 +69,7 @@ const RootPage = () => { const NoLayoutPage = () => { return ( - + ); diff --git a/src/index.tsx b/src/index.tsx index 9fd3f1341..3738e24c7 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -28,6 +28,7 @@ import relativeTimePlugin from "dayjs/plugin/relativeTime"; import localizedFormat from "dayjs/plugin/localizedFormat"; import { CAP_IS_WEB } from "./env"; import { App } from "./app"; +import { logger } from "./helpers/debug"; dayjs.extend(relativeTimePlugin); dayjs.extend(localizedFormat); @@ -41,15 +42,18 @@ if (import.meta.env.PROD) { } } -// if web, register service worker -if (CAP_IS_WEB) { - const { registerServiceWorker } = await import("./services/worker"); - registerServiceWorker(); -} - +logger("Rendering app"); const root = document.getElementById("root")!; createRoot(root).render( , ); + +// // if web, register service worker +// // NOTE: this should happen after the app renders so it does not stop the app from rendering +// if (CAP_IS_WEB) { +// logger("Loading service worker"); +// const { registerServiceWorker } = await import("./services/worker"); +// await registerServiceWorker(); +// } diff --git a/src/services/cache-relay.ts b/src/services/cache-relay.ts index 3a218e70e..0319f9a36 100644 --- a/src/services/cache-relay.ts +++ b/src/services/cache-relay.ts @@ -1,4 +1,4 @@ -import { BehaviorSubject, pairwise } from "rxjs"; +import { BehaviorSubject, distinctUntilChanged, pairwise } from "rxjs"; import { CacheRelay, openDB } from "nostr-idb"; import { AbstractRelay } from "nostr-tools/abstract-relay"; import { fakeVerifyEvent, isFromCache } from "applesauce-core/helpers"; @@ -86,7 +86,7 @@ export const cacheRelay$ = new BehaviorSubject< >(null); // create a new cache relay instance when the url changes -localSettings.cacheRelayURL.subscribe(async (url) => { +localSettings.cacheRelayURL.pipe(distinctUntilChanged()).subscribe(async (url) => { if (cacheRelay$.value && cacheRelay$.value.url === url) return; const relay = await connectRelay(url); diff --git a/src/services/verify-event.ts b/src/services/verify-event.ts index edef7a13b..0846b5869 100644 --- a/src/services/verify-event.ts +++ b/src/services/verify-event.ts @@ -3,6 +3,7 @@ import { setNostrWasm, verifyEvent as wasmVerifyEvent } from "nostr-tools/wasm"; import { fakeVerifyEvent } from "applesauce-core/helpers/event"; import { logger } from "../helpers/debug"; import localSettings from "./local-settings"; +import { distinctUntilChanged } from "rxjs"; const log = logger.extend("VerifyEvent"); let verifyEventMethod: typeof internalVerifyEvent; @@ -64,5 +65,4 @@ async function updateVerifyMethod() { } } -localSettings.verifyEventMethod.subscribe(updateVerifyMethod); -await updateVerifyMethod(); +localSettings.verifyEventMethod.pipe(distinctUntilChanged()).subscribe(updateVerifyMethod); diff --git a/src/services/web-push-notifications.ts b/src/services/web-push-notifications.ts index 5d75b552c..234b879a2 100644 --- a/src/services/web-push-notifications.ts +++ b/src/services/web-push-notifications.ts @@ -1,10 +1,6 @@ -import { type WebPushChannel } from "@satellite-earth/core/types/control-api/notifications.js"; -import { nanoid } from "nanoid"; - -import { getControlApi } from "./bakery"; import { BehaviorSubject } from "rxjs"; + import { serviceWorkerRegistration } from "./worker"; -import localSettings from "./local-settings"; export const pushSubscription = new BehaviorSubject(null); serviceWorkerRegistration.subscribe(async (registration) => { @@ -14,42 +10,36 @@ serviceWorkerRegistration.subscribe(async (registration) => { }); export async function enableNotifications() { - const controlApi = getControlApi(); - - if (!controlApi) throw new Error("Missing control api"); - const subscription = await serviceWorkerRegistration.value?.pushManager.subscribe({ - userVisibleOnly: true, - applicationServerKey: controlApi.vapidKey.value, - }); - - if (subscription) { - const json = subscription.toJSON(); - const { endpoint } = json; - if (!endpoint) throw new Error("Missing endpoint"); - - // @ts-expect-error - const isMobile: boolean = navigator.userAgentData?.mobile ?? navigator.userAgent.includes("Android"); - const metadata: WebPushChannel = { - id: `web:${nanoid()}`, - type: "web", - device: localSettings.deviceId.value, - endpoint: endpoint!, - expirationTime: subscription.expirationTime, - keys: json.keys as WebPushChannel["keys"], - }; - - controlApi.send(["CONTROL", "NOTIFICATIONS", "REGISTER", metadata]); - pushSubscription.next(subscription); - } else throw new Error("Failed to register subscription"); + // const controlApi = getControlApi(); + // if (!controlApi) throw new Error("Missing control api"); + // const subscription = await serviceWorkerRegistration.value?.pushManager.subscribe({ + // userVisibleOnly: true, + // applicationServerKey: controlApi.vapidKey.value, + // }); + // if (subscription) { + // const json = subscription.toJSON(); + // const { endpoint } = json; + // if (!endpoint) throw new Error("Missing endpoint"); + // // @ts-expect-error + // const isMobile: boolean = navigator.userAgentData?.mobile ?? navigator.userAgent.includes("Android"); + // const metadata: WebPushChannel = { + // id: `web:${nanoid()}`, + // type: "web", + // device: localSettings.deviceId.value, + // endpoint: endpoint!, + // expirationTime: subscription.expirationTime, + // keys: json.keys as WebPushChannel["keys"], + // }; + // controlApi.send(["CONTROL", "NOTIFICATIONS", "REGISTER", metadata]); + // pushSubscription.next(subscription); + // } else throw new Error("Failed to register subscription"); } export async function disableNotifications() { - const controlApi = getControlApi(); - - if (pushSubscription.value) { - const key = pushSubscription.value.toJSON().keys?.p256dh; - if (key) controlApi?.send(["CONTROL", "NOTIFICATIONS", "UNREGISTER", key]); - - await pushSubscription.value.unsubscribe(); - } + // const controlApi = getControlApi(); + // if (pushSubscription.value) { + // const key = pushSubscription.value.toJSON().keys?.p256dh; + // if (key) controlApi?.send(["CONTROL", "NOTIFICATIONS", "UNREGISTER", key]); + // await pushSubscription.value.unsubscribe(); + // } } diff --git a/src/services/worker.ts b/src/services/worker.ts index 203ef55d0..9a0541dd4 100644 --- a/src/services/worker.ts +++ b/src/services/worker.ts @@ -1,5 +1,5 @@ import { BehaviorSubject } from "rxjs"; -import { registerSW } from "virtual:pwa-register"; +// import { registerSW } from "virtual:pwa-register"; import { logger } from "../helpers/debug"; @@ -8,24 +8,24 @@ const log = logger.extend("ServiceWorker"); export const serviceWorkerRegistration = new BehaviorSubject(null); export async function registerServiceWorker() { - if (serviceWorkerRegistration.value) return; - - await registerSW({ - immediate: true, - onRegisteredSW: (s, r) => { - if (r) serviceWorkerRegistration.next(r); - - if (import.meta.env.DEV) { - // @ts-expect-error - window.serviceWorker = r; - } - }, - onOfflineReady() { - log("Offline ready"); - }, - onRegisterError(error) { - log("Failed to register service worker"); - log(error); - }, - }); + // NOTE: temporarily disabled because of bug with vite-pwa-plugin registering service worker + // if (serviceWorkerRegistration.value) return; + // log("Registering service worker"); + // await registerSW({ + // immediate: true, + // onRegisteredSW: (s, r) => { + // if (r) serviceWorkerRegistration.next(r); + // if (import.meta.env.DEV) { + // // @ts-expect-error + // window.serviceWorker = r; + // } + // }, + // onOfflineReady() { + // log("Offline ready"); + // }, + // onRegisterError(error) { + // log("Failed to register service worker"); + // log(error); + // }, + // }); } diff --git a/src/worker.ts b/src/worker.ts index af47f2ebd..274dd9c5a 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -1,4 +1,4 @@ -// / +/// /// /// /// diff --git a/vite.config.ts b/vite.config.ts index d1ef6d6d4..6766918dd 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -14,22 +14,17 @@ export default defineConfig({ build: { target: ["chrome89", "edge89", "firefox89", "safari15"], sourcemap: true, - rollupOptions: { - output: { - // don't create any chunks smaller than 500kB - experimentalMinChunkSize: 1024 * 100, - }, - }, + minify: false, }, plugins: [ react(), tsconfigPaths(), VitePWA({ - strategies: "injectManifest", + // strategies: "injectManifest", + // srcDir: "src", + // filename: "worker.ts", registerType: "autoUpdate", - injectRegister: null, - srcDir: "src", - filename: "worker.ts", + // injectRegister: null, injectManifest: { minify: false, sourcemap: true,