Added logic to allow refreshing the PWA without closing the tabs

This commit is contained in:
Alejandro Celaya
2021-07-12 16:16:18 +02:00
parent 08694d7693
commit 69905c4b38
3 changed files with 43 additions and 8 deletions

16
src/utils/helpers/sw.ts Normal file
View File

@@ -0,0 +1,16 @@
export const forceUpdate = async () => {
const registrations = await navigator.serviceWorker?.getRegistrations() ?? [];
for (const registration of registrations) {
const { waiting } = registration;
waiting?.addEventListener('statechange', (event) => {
if ((event.target as any)?.state === 'activated') {
window.location.reload();
}
});
// The logic that makes skipWaiting to be called when this message is posted is in service-worker.ts
waiting?.postMessage({ type: 'SKIP_WAITING' });
}
};