tmp disable custom service worker

This commit is contained in:
hzrd149 2025-01-20 19:07:46 -06:00
parent 16866de7ec
commit 07d2df7d5c
8 changed files with 74 additions and 85 deletions

View File

@ -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 (
<RouteProviders>
<ScrollRestoration getKey={getScrollKey} />
<ScrollRestoration />
<AppLayout />
</RouteProviders>
);
@ -69,7 +69,7 @@ const RootPage = () => {
const NoLayoutPage = () => {
return (
<RouteProviders>
<ScrollRestoration getKey={getScrollKey} />
<ScrollRestoration />
<Outlet />
</RouteProviders>
);

View File

@ -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(
<GlobalProviders>
<App />
</GlobalProviders>,
);
// // 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();
// }

View File

@ -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);

View File

@ -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);

View File

@ -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<PushSubscription | null>(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();
// }
}

View File

@ -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<ServiceWorkerRegistration | null>(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);
// },
// });
}

View File

@ -1,4 +1,4 @@
// / <reference no-default-lib="true"/>
/// <reference no-default-lib="true"/>
/// <reference lib="ES2022" />
/// <reference lib="DOM" />
/// <reference lib="webworker" />

View File

@ -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,