shellphone.app/app/features/core/components/service-worker-update-notifier.tsx
m5r 1e9b7a8aa2 * fix "dev:build" watch mode
* remove cross-env
* append build hash to service worker cache names for easy purge
2022-06-11 15:13:28 +02:00

59 lines
1.4 KiB
TypeScript

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 (
<div className="absolute inset-0">
<button
onClick={() => {
setHasUpdate(false);
location.reload();
}}
title="An updated version of the app is available. Reload to get the latest version."
>
<IoDownloadOutline
className="-ml-1 mr-2 h-5 w-5"
aria-hidden="true"
aria-label="An updated version of the app is available. Reload to get the latest version."
/>
</button>
</div>
);
}