From 53de29f263e1abbebb1e553a3e78d53338086d4a Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Tue, 9 Apr 2024 23:37:41 +0200 Subject: [PATCH] Unregister existing service workers (#722) In case some other app was served on the same domain which registered a service worker, this service worker was incorrectly used and never unregistered --- src/index.tsx | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/index.tsx b/src/index.tsx index ea09f773..b72514a4 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -12,6 +12,18 @@ import { App } from '@/App'; import '@/index.css'; // roboto font import '@fontsource/roboto'; +import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts'; + +if ('serviceWorker' in navigator) { + navigator.serviceWorker.ready.then((registration) => { + registration.unregister().catch(defaultPromiseErrorHandler('unregister service workers')); + if (caches) { + caches.keys().then(async (names) => { + await Promise.all(names.map((name) => caches.delete(name))); + }); + } + }); +} const container = document.getElementById('root'); const root = createRoot(container!);