diff --git a/app/features/core/components/service-worker-update-notifier.tsx b/app/features/core/components/service-worker-update-notifier.tsx new file mode 100644 index 0000000..b873807 --- /dev/null +++ b/app/features/core/components/service-worker-update-notifier.tsx @@ -0,0 +1,59 @@ +import { useEffect, useState } from "react"; +import { IoDownloadOutline } from "react-icons/io5"; + +export default function ServiceWorkerUpdateNotifier() { + const [hasUpdate, setHasUpdate] = useState(false); + useEffect(() => { + (async () => { + const registration = await navigator.serviceWorker.getRegistration(); + if (!registration) { + return; + } + + registration.addEventListener( + "updatefound", + () => { + console.debug("Service Worker update detected"); + const installingWorker = registration.installing; + if (!installingWorker) { + return; + } + + installingWorker.addEventListener("statechange", () => { + if (installingWorker.state !== "activated") { + // should maybe retry if state === "redundant" + console.debug(`Service worker state changed to ${installingWorker.state}`); + return; + } + + setHasUpdate(true); + }); + }, + { once: true }, + ); + })(); + }, []); + + if (!hasUpdate) { + return null; + } + + return ( +