From cc731c15f1d72ce4d21a7958ca15128ae1dcdd89 Mon Sep 17 00:00:00 2001 From: wermanoid Date: Wed, 2 Oct 2024 21:10:48 +0200 Subject: [PATCH] fix(oidc): force sw update when new registration appears (release) (#1449) --- examples/react-oidc-demo/package.json | 3 ++- packages/oidc-client/src/initWorker.ts | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/examples/react-oidc-demo/package.json b/examples/react-oidc-demo/package.json index 1385c0cdc..54ad7d769 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 3ebbf95fe..56443a7f7 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