diff --git a/examples/react-oidc-demo/package.json b/examples/react-oidc-demo/package.json index 1385c0cd..54ad7d76 100644 --- a/examples/react-oidc-demo/package.json +++ b/examples/react-oidc-demo/package.json @@ -11,7 +11,8 @@ "url": "https://github.com/AxaGuilDEv/react-oidc.git" }, "scripts": { - "start": "vite", + "start": "pnpm prestart && pnpm vite", + "prestart": "node ./node_modules/@axa-fr/react-oidc/bin/copy-service-worker-files.mjs public", "build": "vite build", "serve": "vite preview", "clean": "rimraf dist", diff --git a/packages/oidc-client/src/initWorker.ts b/packages/oidc-client/src/initWorker.ts index 3ebbf95f..56443a7f 100644 --- a/packages/oidc-client/src/initWorker.ts +++ b/packages/oidc-client/src/initWorker.ts @@ -113,6 +113,11 @@ export const initWorkerAsync = async ( registration = await configuration.service_worker_register(serviceWorkerRelativeUrl); } else { registration = await navigator.serviceWorker.register(serviceWorkerRelativeUrl); + + if (registration.active && registration.waiting) { + console.log('Detected new service worker waiting, unregistering and reloading'); + await configuration.service_worker_update_require_callback?.(registration, stopKeepAlive); + } } try { @@ -151,7 +156,7 @@ export const initWorkerAsync = async ( console.warn( `Service worker ${serviceWorkerVersion} version mismatch with js client version ${codeVersion}, unregistering and reloading`, ); - await oidcConfiguration.service_worker_update_require_callback(registration, stopKeepAlive); + await oidcConfiguration.service_worker_update_require_callback?.(registration, stopKeepAlive); } // @ts-ignore