@@ -70,11 +70,42 @@ export class Project implements ProjectEndpoint {
70
70
if ( this . serviceWorkerOptions ) {
71
71
const { url, scope } = this . serviceWorkerOptions ;
72
72
// Should add "Service-Worker-Allowed": "/" in page root response headers,
73
- await navigator . serviceWorker . register ( url , { scope : "/" } ) ;
73
+ const registration = await navigator . serviceWorker . register ( url , {
74
+ scope : "/" ,
75
+ } ) ;
74
76
75
- navigator . serviceWorker . controller ?. postMessage ( {
76
- [ ServiceWorkerHandShake ] : true ,
77
- scope,
77
+ return new Promise < void > ( ( resolve ) => {
78
+ function sendMessage ( sw : ServiceWorker ) {
79
+ sw . postMessage ( {
80
+ [ ServiceWorkerHandShake ] : true ,
81
+ scope,
82
+ } ) ;
83
+ resolve ( ) ;
84
+ }
85
+
86
+ function listenForActivation ( sw : ServiceWorker ) {
87
+ sw . addEventListener ( "statechange" , ( ) => {
88
+ if ( sw . state === "activated" ) {
89
+ sendMessage ( sw ) ;
90
+ }
91
+ } ) ;
92
+ }
93
+
94
+ function checkSWState ( registration : ServiceWorkerRegistration ) {
95
+ if ( registration . active ) {
96
+ sendMessage ( registration . active ) ;
97
+ } else if ( registration . installing ) {
98
+ listenForActivation ( registration . installing ) ;
99
+ }
100
+
101
+ registration . addEventListener ( "updatefound" , ( ) => {
102
+ if ( registration . installing ) {
103
+ listenForActivation ( registration . installing ) ;
104
+ }
105
+ } ) ;
106
+ }
107
+
108
+ checkSWState ( registration ) ;
78
109
} ) ;
79
110
}
80
111
}
0 commit comments