From 125e645ffd46a5d39fa6e7b2b267aad56db9fb47 Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Tue, 22 Apr 2025 16:50:42 +0200 Subject: [PATCH] fix(pwa): rewrite and update service worker related to fit recent refactors --- components/ProgressiveWebApp.tsx | 5 -- deno.json | 1 + islands/RegisterServiceWorker.tsx | 74 ++++++++++++++------------- islands/StartServiceWorker.tsx | 10 ---- routes/_app.tsx | 4 +- routes/_middleware.ts | 2 - routes/api/serviceworker/precache.tsx | 33 ++++-------- src/serviceworker/middleware.ts | 12 ----- src/serviceworker/mod.ts | 10 +++- 9 files changed, 61 insertions(+), 90 deletions(-) delete mode 100644 components/ProgressiveWebApp.tsx delete mode 100644 islands/StartServiceWorker.tsx delete mode 100644 src/serviceworker/middleware.ts diff --git a/components/ProgressiveWebApp.tsx b/components/ProgressiveWebApp.tsx deleted file mode 100644 index c438f32..0000000 --- a/components/ProgressiveWebApp.tsx +++ /dev/null @@ -1,5 +0,0 @@ -import RegisterServiceWorker from ':islands/RegisterServiceWorker.tsx' - -export function ProgressiveWebApp() { - return -} diff --git a/deno.json b/deno.json index d81f34d..e8b76ef 100644 --- a/deno.json +++ b/deno.json @@ -26,6 +26,7 @@ "packages/" ], "imports": { + "@deno/emit": "jsr:@deno/emit@^0.46.0", "@deno/gfm": "jsr:@deno/gfm@^0.10.0", "@std/fs": "jsr:@std/fs@^1.0.6", "@std/path": "jsr:@std/path@^1.0.8", diff --git a/islands/RegisterServiceWorker.tsx b/islands/RegisterServiceWorker.tsx index 3c4eaf5..efb5e85 100644 --- a/islands/RegisterServiceWorker.tsx +++ b/islands/RegisterServiceWorker.tsx @@ -1,47 +1,49 @@ import { requestApi } from ':src/utils.ts' +import { useEffect } from 'preact/hooks' -export default function RegisterServiceWorker() { - if ('serviceWorker' in navigator) { - import(':islands/StartServiceWorker.tsx').then(async (mod) => { - const href = mod.default() - const registration = await navigator.serviceWorker.register(href, { - scope: '/', - type: 'module', - }) +async function register() { + const registration = await navigator.serviceWorker.register('/sw', { + scope: '/', + type: 'module', + }) - // Notification.requestPermission().then((permission) => { - // if (permission !== 'granted') return + // Notification.requestPermission().then((permission) => { + // if (permission !== 'granted') return - // registration.showNotification('Notification permission granted', { - // body: 'Notification is ok.', - // }) - // }) + // registration.showNotification('Notification permission granted', { + // body: 'Notification is ok.', + // }) + // }) - async function getSubscription() { - const currentSubscription = await registration.pushManager - .getSubscription() - if (currentSubscription) return currentSubscription + async function getSubscription() { + const currentSubscription = await registration.pushManager + .getSubscription() + if (currentSubscription) return currentSubscription - const applicationServerKey = await requestApi( - 'webpush/vapid', - 'GET', - ) + const applicationServerKey = await requestApi( + 'webpush/vapid', + 'GET', + ) - return await registration.pushManager.subscribe({ - userVisibleOnly: true, - applicationServerKey, - }) - } - - try { - if (registration.active === null) return - const subscription = await getSubscription() - await requestApi('webpush/subscription', 'POST', subscription) - } catch (cause) { - console.error('Push subscription is not available', { cause }) - } + return await registration.pushManager.subscribe({ + userVisibleOnly: true, + applicationServerKey, }) } - return <> + try { + if (registration.active === null) return + const subscription = await getSubscription() + await requestApi('webpush/subscription', 'POST', subscription) + } catch (cause) { + console.error('Push subscription is not available', { cause }) + } +} + +export default function RegisterServiceWorker() { + useEffect(() => { + if ('serviceWorker' in navigator) register() + }, []) + + return null } diff --git a/islands/StartServiceWorker.tsx b/islands/StartServiceWorker.tsx deleted file mode 100644 index ffcf7c2..0000000 --- a/islands/StartServiceWorker.tsx +++ /dev/null @@ -1,10 +0,0 @@ -import { main } from ':src/serviceworker/mod.ts' - -const IS_SW = 'onpushsubscriptionchange' in self - -export default function StartServiceWorker() { - if (IS_SW) { - main() - } - return new URL(import.meta.url).pathname -} diff --git a/routes/_app.tsx b/routes/_app.tsx index ac42fca..852848c 100644 --- a/routes/_app.tsx +++ b/routes/_app.tsx @@ -2,8 +2,8 @@ import { asset, Partial } from 'fresh/runtime' import { type PageProps } from 'fresh' import { Footer } from ':components/Footer.tsx' import { Header } from ':components/Header.tsx' -import { ProgressiveWebApp } from ':components/ProgressiveWebApp.tsx' import IsOnline from ':islands/IsOnline.tsx' +import RegisterServiceWorker from ':islands/RegisterServiceWorker.tsx' export default function App( { Component, data }: PageProps<{ title?: string } | undefined>, @@ -54,7 +54,7 @@ export default function App( - +