Skip to content
This repository was archived by the owner on Apr 21, 2025. It is now read-only.

Commit cd8fe6c

Browse files
committed
WIP: service worker
1 parent 5e1b97d commit cd8fe6c

File tree

4 files changed

+111
-7
lines changed

4 files changed

+111
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"@kobalte/tailwindcss": "^0.5.0",
5454
"@modular-forms/solid": "^0.18.1",
5555
"@mutinywallet/barcode-scanner": "5.0.0-beta.3",
56-
"@mutinywallet/mutiny-wasm": "0.4.23",
56+
"@mutinywallet/mutiny-wasm": "^0.4.23",
5757
"@mutinywallet/waila-wasm": "^0.2.1",
5858
"@nostr-dev-kit/ndk": "^0.8.11",
5959
"@solid-primitives/upload": "^0.0.111",

public/sw.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// /// <reference lib="WebWorker" />
2+
// /// <reference types="vite/client" />
3+
//
4+
// import initMutinyWallet, { MutinyWallet } from "@mutinywallet/mutiny-wasm";
5+
//
6+
// // SERVICE WORKER SETUP STUFF
7+
// declare let self: ServiceWorkerGlobalScope
8+
//
9+
// const entries = self.__WB_MANIFEST
10+
//
11+
// if (import.meta.env.DEV)
12+
// entries.push({ url: '/', revision: Math.random().toString() })
13+
//
14+
// // allow only fallback in dev: we don't want to cache anything
15+
// let allowlist: undefined | RegExp[]
16+
// if (import.meta.env.DEV)
17+
// allowlist = [/^\/$/]
18+
//
19+
// // deny api and server page calls
20+
// let denylist: undefined | RegExp[]
21+
// if (import.meta.env.PROD) {
22+
// denylist = [
23+
// /^\/sw.js$/,
24+
// // exclude webmanifest: has its own cache
25+
// /^\/manifest-(.*).webmanifest$/,
26+
// ]
27+
// }
28+
//
29+
//
30+
// ACTUAL LOGIC
31+
console.log("hello from the service worker?")
32+
33+
self.addEventListener('push', (event) => {
34+
console.log("push event", event)
35+
const data = event.data.json();
36+
const options = {
37+
body: data.body,
38+
icon: data.icon,
39+
badge: data.badge
40+
};
41+
event.waitUntil(
42+
self.registration.showNotification(data.title, options)
43+
);
44+
});
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+

src/components/ResetRouter.tsx

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,63 @@
11
import { Button, InnerCard, NiceP, VStack } from "~/components";
22
import { useI18n } from "~/i18n/context";
33
import { useMegaStore } from "~/state/megaStore";
4+
import {MutinyWallet} from "@mutinywallet/mutiny-wasm";
5+
6+
// if ('serviceWorker' in navigator) {
7+
// navigator.serviceWorker.register('sw.ts').then(registration => {
8+
// console.log('Service Worker registered with scope:', registration.scope);
9+
// }).catch(error => {
10+
// console.error('Service Worker registration failed:', error);
11+
// });
12+
// }
13+
14+
async function subscribeUserToPush() {
15+
console.log("waiting for service worker");
16+
const registration = await navigator.serviceWorker.ready;
17+
console.log("using push manager");
18+
const subscription = await registration.pushManager.subscribe({
19+
userVisibleOnly: true,
20+
applicationServerKey: urlBase64ToUint8Array('BJbNCspGEEdyrj4hI6DD5OBlXpEgVzfaWwZP72p0EiSUTQKXyWauOKGzi-_NWq0dT31s3r5MRPvYVeussdEBygM')
21+
});
22+
console.log("talking to mutiny notification service");
23+
try {
24+
// Send the subscription to your server
25+
await MutinyWallet.test_register_web_push("https://auth-staging.mutinywallet.com", "http://localhost:8080", JSON.stringify(subscription));
26+
console.log("registered")
27+
} catch (e) {
28+
console.error(e)
29+
}
30+
}
31+
32+
function urlBase64ToUint8Array(base64String) {
33+
const padding = '='.repeat((4 - base64String.length % 4) % 4);
34+
const base64 = (base64String + padding)
35+
.replace(/\-/g, '+')
36+
.replace(/_/g, '/');
37+
const rawData = window.atob(base64);
38+
const outputArray = new Uint8Array(rawData.length);
39+
for (let i = 0; i < rawData.length; ++i) {
40+
outputArray[i] = rawData.charCodeAt(i);
41+
}
42+
return outputArray;
43+
}
44+
445

546
export function ResetRouter() {
647
const i18n = useI18n();
748
const [state, _] = useMegaStore();
849

950
async function reset() {
10-
try {
11-
await state.mutiny_wallet?.reset_router();
12-
} catch (e) {
13-
console.error(e);
14-
}
51+
// Request notification permission from the user
52+
Notification.requestPermission().then(async permission => {
53+
if (permission === 'granted') {
54+
console.log('Notification permission granted.');
55+
await subscribeUserToPush();
56+
console.log('subscribed.');
57+
} else {
58+
console.error('Notification permission denied.');
59+
}
60+
});
1561
}
1662

1763
return (

vite.config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ const commitHash = child.execSync("git rev-parse --short HEAD").toString().trim(
1313

1414
const pwaOptions: Partial<VitePWAOptions> = {
1515
base: "/",
16+
injectRegister: 'inline',
17+
filename: 'sw.ts',
18+
strategies: 'injectManifest',
1619
registerType: "autoUpdate",
1720
devOptions: {
18-
enabled: false
21+
enabled: true,
22+
type: "module"
1923
},
2024
includeAssets: ["favicon.ico", "robots.txt"],
2125
manifest: manifest

0 commit comments

Comments
 (0)