@@ -40,7 +40,7 @@ function showRefreshUI(registration) {
40
40
41
41
button .disabled = true ;
42
42
43
- registration .waiting .postMessage (' force-activate ' );
43
+ registration .waiting .postMessage (' skipWaiting ' );
44
44
});
45
45
46
46
document .body .appendChild (button);
@@ -54,7 +54,7 @@ function onNewServiceWorker(registration, callback) {
54
54
}
55
55
56
56
function listenInstalledStateChange () {
57
- registration .installing .addEventListener (' statechange' , function () {
57
+ registration .installing .addEventListener (' statechange' , function (event ) {
58
58
if (event .target .state === ' installed' ) {
59
59
// A new service worker is available, inform the user
60
60
callback ();
@@ -73,19 +73,9 @@ function onNewServiceWorker(registration, callback) {
73
73
74
74
window .addEventListener (' load' , function () {
75
75
// When the user asks to refresh the UI, we'll need to reload the window
76
- navigator .serviceWorker .addEventListener (' message' , function (event ) {
77
- if (! event .data ) {
78
- return ;
79
- }
80
-
81
- switch (event .data ) {
82
- case ' reload-window' :
83
- window .location .reload ();
84
- break ;
85
- default :
86
- // NOOP
87
- break ;
88
- }
76
+ navigator .serviceWorker .addEventListener (' controllerchange' , function (event ) {
77
+ console .log (' Controller loaded' );
78
+ window .location .reload ();
89
79
});
90
80
91
81
navigator .serviceWorker .register (' /sw.js' )
@@ -108,11 +98,10 @@ This code handles the verious possible lifecycles of the service worker
108
98
and detects when a new service worker has become installed and is waiting to
109
99
activate.
110
100
111
- When a waiting service worker is found we set up a message listener on
112
- ` navigator.serviceWorker ` so we know when to reload the window. When the
101
+ When a waiting service worker is found we set up a 'controllerchange' listener
102
+ on ` navigator.serviceWorker ` so we know when to reload the window. When the
113
103
user clicks on the UI to refresh the page, we post a message to the new
114
- service worker telling it to force an activation. After this a message is
115
- posted to all windows telling them to reload.
104
+ service worker telling it to ` skipWaiting ` meaning it'll start to activate.
116
105
117
106
Note: This is one possible approach to refreshing the page on a new service
118
107
worker. For a more thorough answer as well as an explanation of alternative
@@ -129,12 +118,8 @@ self.addEventListener('message', (event) => {
129
118
}
130
119
131
120
switch (event .data ) {
132
- case ' force-activate ' :
121
+ case ' skipWaiting ' :
133
122
self .skipWaiting ();
134
- self .clients .claim ();
135
- self .clients .matchAll ().then ((clients ) => {
136
- clients .forEach ((client ) => client .postMessage (' reload-window' ));
137
- });
138
123
break ;
139
124
default :
140
125
// NOOP
@@ -143,10 +128,8 @@ self.addEventListener('message', (event) => {
143
128
});
144
129
```
145
130
146
- This will receive a the 'force-activate' message and call ` skipWaiting() ` and
147
- ` clients.claim() ` so that the service worker activates immediately and controls
148
- all of the currently open windows. We then message each window with a
149
- 'reload-window' message so each tab is refreshed with the latest content.
131
+ This will receive a the 'skipWaiting' message and call ` skipWaiting() ` forcing
132
+ the service worker to activate immediately.
150
133
151
134
## "Warm" the runtime cache
152
135
0 commit comments