Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: service worker #606

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@
"@capacitor/clipboard": "^5.0.6",
"@capacitor/core": "^5.2.2",
"@capacitor/filesystem": "^5.1.4",
"@capacitor/share": "^5.0.6",
"@capacitor/haptics": "^5.0.6",
"@capacitor/share": "^5.0.6",
"@capacitor/toast": "^5.0.6",
"@kobalte/core": "^0.9.8",
"@kobalte/tailwindcss": "^0.5.0",
"@modular-forms/solid": "^0.18.1",
"@mutinywallet/barcode-scanner": "5.0.0-beta.3",
"@mutinywallet/mutiny-wasm": "0.4.25",
"@mutinywallet/mutiny-wasm": "^0.4.25",
"@mutinywallet/waila-wasm": "^0.2.1",
"@nostr-dev-kit/ndk": "^0.8.11",
"@solid-primitives/upload": "^0.0.111",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions public/sw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// /// <reference lib="WebWorker" />
// /// <reference types="vite/client" />
//
// import initMutinyWallet, { MutinyWallet } from "@mutinywallet/mutiny-wasm";
//
// // SERVICE WORKER SETUP STUFF
// declare let self: ServiceWorkerGlobalScope
//
// const entries = self.__WB_MANIFEST
//
// if (import.meta.env.DEV)
// entries.push({ url: '/', revision: Math.random().toString() })
//
// // allow only fallback in dev: we don't want to cache anything
// let allowlist: undefined | RegExp[]
// if (import.meta.env.DEV)
// allowlist = [/^\/$/]
//
// // deny api and server page calls
// let denylist: undefined | RegExp[]
// if (import.meta.env.PROD) {
// denylist = [
// /^\/sw.js$/,
// // exclude webmanifest: has its own cache
// /^\/manifest-(.*).webmanifest$/,
// ]
// }
//
//
// ACTUAL LOGIC
console.log("hello from the service worker?")

self.addEventListener('push', (event) => {
console.log("push event", event)
if (!(self.Notification && self.Notification.permission === "granted")) {
return;
}

const data = event.data?.json() ?? {};
const options = {
body: data.body,
icon: data.icon,
badge: data.badge
};
event.waitUntil(
self.registration.showNotification(data.title, options)
);
});
52 changes: 47 additions & 5 deletions src/components/ResetRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,59 @@
import { Button, InnerCard, NiceP, VStack } from "~/components";
import { useI18n } from "~/i18n/context";
import { useMegaStore } from "~/state/megaStore";
import {MutinyWallet} from "@mutinywallet/mutiny-wasm";

async function subscribeUserToPush(mutinyWallet: MutinyWallet) {
console.log("waiting for service worker");
const registration = await navigator.serviceWorker.ready;
console.log("using push manager");
const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array('BJbNCspGEEdyrj4hI6DD5OBlXpEgVzfaWwZP72p0EiSUTQKXyWauOKGzi-_NWq0dT31s3r5MRPvYVeussdEBygM')
});
console.log("talking to mutiny notification service");
try {
// Send the subscription to your server
await mutinyWallet.register_web_push(subscription);
console.log("registered")
} catch (e) {
console.error(e)
}
}

function urlBase64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}


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

async function reset() {
try {
await state.mutiny_wallet?.reset_router();
} catch (e) {
console.error(e);
}
// Request notification permission from the user
Notification.requestPermission().then(async permission => {
if (permission === 'granted') {
console.log('Notification permission granted.');
if (state.mutiny_wallet) {
await subscribeUserToPush(state.mutiny_wallet);
console.log('subscribed.');
} else {
console.error('no mutiny wallet');
}
} else {
console.error('Notification permission denied.');
}
});
}

return (
Expand Down
1 change: 1 addition & 0 deletions src/logic/mutinyWalletSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export async function setupMutinyWallet(
subscriptions,
storage,
scorer,
"http://127.0.0.1:8080",
// Do not connect peers
undefined,
// Do not skip device lock
Expand Down
3 changes: 3 additions & 0 deletions src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ import {
import { useI18n } from "~/i18n/context";
import { FeedbackLink } from "~/routes/Feedback";
import { useMegaStore } from "~/state/megaStore";
import { registerSW } from 'virtual:pwa-register';

export default function App() {
registerSW({ immediate: true });

const i18n = useI18n();
const [state, _actions] = useMegaStore();

Expand Down
6 changes: 5 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ const commitHash = process.env.VITE_COMMIT_HASH ?? child.execSync("git rev-parse

const pwaOptions: Partial<VitePWAOptions> = {
base: "/",
injectRegister: 'inline',
filename: 'sw.ts',
strategies: 'injectManifest',
registerType: "autoUpdate",
devOptions: {
enabled: false
enabled: true,
type: "module"
},
includeAssets: ["favicon.ico", "robots.txt"],
manifest: manifest
Expand Down
Loading