Document the event handler created by workbox-core's clientsClaim()
See original GitHub issueLibrary Affected: workbox-sw, workbox-window
Browser & Platform: all browsers
Issue or Feature Request Description:
I’m following the “Offer a page reload for users” from “Advanced Recipes”. The sample code suggest to listen to the WB controlling
event and then perform the reload. This event is never triggered for me.
When I change it to activated
it works.
Is this an error in the docs or am I doing something wrong?
When reporting bugs, please include relevant JavaScript Console logs and links to public URLs at which the issue could be reproduced.
service-worker.js
:
// …
addEventListener('message', event => {
if (event.data && event.data.type === 'SKIP_WAITING') {
skipWaiting();
}
});
// …
service-worker-registration.js
:
const wb = new Workbox('/sw.js');
wb.addEventListener('waiting', event => {
if (alert('Everything is new. Wanna see?')) {
wb.addEventListener('controlling', event => {
// will never be called
window.location.reload();
}
wb.messageSW({ type: 'SKIP_WAITING' });
}
});
wb.register();
Issue Analytics
- State:
- Created 4 years ago
- Comments:16 (8 by maintainers)
Top Results From Across the Web
workbox-core - Chrome Developers
The clientsClaim() method in workbox-core automatically adds an activate event listener to your service worker, and inside of it, ...
Read more >Clients.claim() - Web APIs - MDN Web Docs
A Promise that resolves to undefined . Examples. The following example uses claim() inside service worker's " activate " event listener so that ......
Read more >What are the downsides to using skipWaiting and clientsClaim ...
Workbox uses an install event handler to cache new or updated entries in the precache manifest (appending a __WB_REVISION__ query param to ...
Read more >Handling JavaFX Events: Working with Event Handlers
Learn how event handlers can be used to process the events generated by ... The handle() method of this interface provides the code...
Read more >Service Workers - W3C
This document was produced by a group operating under the W3C Patent ... A service worker has an associated set of event types...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@hannandesai: Just a few random comments:
I had some issues getting your code to compile as-is, because you’re doing things like
const path = require("path");
inside of a service worker, which won’t work. I’m assuming you have something custom in your build process that somehow translates that into code that will run inside of theServiceWorkerGlobalScope
, but I’m not sure what it is. So I pared down your sample code a bit instead of running it directly.But in any case, I see some problems with the following code:
In this case,
serviceWorker.waiting
is an instance ofServiceWorker
, not ofWorkbox
. There’s no event calledcontrolling
that is ever fired on aServiceWorker
, so listening for that won’t accomplish anything.A
Workbox
object can listen forcontrolling
events, though (it’s one of the “synthetic” events thatworkbox-window
dispatches), so that’s what I assume you intended to do.In the course of exploring this issue, though, I realized that the
Workbox
object’scontrolling
event isn’t dispatched if the new service worker that takes control isn’t the same as the service worker that originated from theregister()
method. That’s a bug—we should firecontrolling
in either case, with theisExternal
property set totrue
orfalse
—and I created #2786 to track that.So, once #2768 is resolved, you should be able to write:
In the meantime, instead of using
wb.addEventListener('controlling', () => {...})
, you could just use the underlying service worker API directly to listen for thecontrollerchange
event conditionally, only if there’s already a service worker in control (to avoid firing it when it’s the initial service worker):The rest of the general logic could remain the same in that case.
Yup, understood. I will dive into both code samples soon. I just know that
clientsClaim()
can be confusing to use and wanted to clarify that off the bat.