@@ -634,8 +634,6 @@ public async Task RegisterServiceWorker()
634634 {
635635 ServiceWorkerConfig . ScriptURL = WebWorkerJSScript ;
636636 }
637- using var navigator = JS . WindowThis . Navigator ;
638- using var serviceWorker = navigator . ServiceWorker ;
639637 var kvps = new Dictionary < string , string > ( ) ;
640638 if ( ServiceWorkerConfig . ImportServiceWorkerAssets )
641639 {
@@ -659,11 +657,27 @@ public async Task RegisterServiceWorker()
659657 {
660658 workerUrl += $ "?{ queryStr } ";
661659 }
662- using var registration = await serviceWorker . Register ( workerUrl , ServiceWorkerConfig . Options ) ;
663- OnServiceWorkerRegistered ? . Invoke ( registration ) ;
664- registration . OnUpdateFound += ServiceWorker_OnUpdateFound ;
660+ using var navigator = JS . Get < Navigator > ( "navigator" ) ;
661+ using var serviceWorkerContainer = navigator . ServiceWorker ;
662+ var serviceWorkerRegistration = await serviceWorkerContainer . Register ( workerUrl , ServiceWorkerConfig . Options ) ;
663+ ServiceWorkerChanged ( serviceWorkerRegistration ) ;
664+ }
665+ }
666+ void ServiceWorkerChanged ( ServiceWorkerRegistration serviceWorkerRegistration )
667+ {
668+ if ( serviceWorkerRegistration == null ) return ;
669+ if ( ActiveServiceWorkerRegistration != null )
670+ {
671+ ActiveServiceWorkerRegistration . OnUpdateFound -= ServiceWorker_OnUpdateFound ;
665672 }
673+ ActiveServiceWorkerRegistration = serviceWorkerRegistration ;
674+ ActiveServiceWorkerRegistration . OnUpdateFound += ServiceWorker_OnUpdateFound ;
675+ OnServiceWorkerRegistered ? . Invoke ( ActiveServiceWorkerRegistration ) ;
666676 }
677+ /// <summary>
678+ /// Returns the active ServiceWorkerRegistration if one has been registered using the WebWorkerService RegisterServiceWorker method.
679+ /// </summary>
680+ public ServiceWorkerRegistration ? ActiveServiceWorkerRegistration { get ; private set ; }
667681 void ServiceWorker_OnUpdateFound ( )
668682 {
669683 OnServiceWorkerUpdateFound ? . Invoke ( ) ;
0 commit comments