Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit 86e55d7

Browse files
Matt Gauntpetele
Matt Gaunt
authored andcommitted
changing update on reload recipe (#5505)
* changing update on reload recipe * Found event wasn't existing * Brain is failing me * Skip
1 parent 819eaaf commit 86e55d7

File tree

1 file changed

+11
-28
lines changed

1 file changed

+11
-28
lines changed

src/content/en/tools/workbox/guides/advanced-recipes.md

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function showRefreshUI(registration) {
4040

4141
button.disabled = true;
4242

43-
registration.waiting.postMessage('force-activate');
43+
registration.waiting.postMessage('skipWaiting');
4444
});
4545

4646
document.body.appendChild(button);
@@ -54,7 +54,7 @@ function onNewServiceWorker(registration, callback) {
5454
}
5555

5656
function listenInstalledStateChange() {
57-
registration.installing.addEventListener('statechange', function() {
57+
registration.installing.addEventListener('statechange', function(event) {
5858
if (event.target.state === 'installed') {
5959
// A new service worker is available, inform the user
6060
callback();
@@ -73,19 +73,9 @@ function onNewServiceWorker(registration, callback) {
7373

7474
window.addEventListener('load', function() {
7575
// 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();
8979
});
9080

9181
navigator.serviceWorker.register('/sw.js')
@@ -108,11 +98,10 @@ This code handles the verious possible lifecycles of the service worker
10898
and detects when a new service worker has become installed and is waiting to
10999
activate.
110100

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
113103
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.
116105

117106
Note: This is one possible approach to refreshing the page on a new service
118107
worker. For a more thorough answer as well as an explanation of alternative
@@ -129,12 +118,8 @@ self.addEventListener('message', (event) => {
129118
}
130119

131120
switch (event.data) {
132-
case 'force-activate':
121+
case 'skipWaiting':
133122
self.skipWaiting();
134-
self.clients.claim();
135-
self.clients.matchAll().then((clients) => {
136-
clients.forEach((client) => client.postMessage('reload-window'));
137-
});
138123
break;
139124
default:
140125
// NOOP
@@ -143,10 +128,8 @@ self.addEventListener('message', (event) => {
143128
});
144129
```
145130

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.
150133

151134
## "Warm" the runtime cache
152135

0 commit comments

Comments
 (0)