Im trying to understand the difference between skipWaiting
and clientsClaim
. In my understanding: calling skipWaiting
will cause the new service worker to skip the waiting phase, and become active right away. clientsClaim
can then 'claim' any other open tabs as well.
What I gather from documentation online:
skipWaiting
skips the waiting phase, and becomes active right away source
clientsClaim
immediately start controlling pages source
In every post I find online, I usually always see clientsClaim
and skipWaiting
used together.
However, I recently found a service worker that only uses clientsClaim
, and I'm having a hard time wrapping my head around what actually is the difference between clientsClaim
and skipWaiting
, and in what scenario do you use clientsClaim
but not skipWaiting
?
My thinking on this, and this may be where I'm wrong, but this is my understanding of it:
Is that calling clientsClaim
, but not skipWaiting
is redundant? Considering:
skipWaiting
)clientsClaim
, even though we just closed all open pages to even activate the new service worker. There should be no other pages to control, because we just closed them.Could someone help me understand?
Read documentation on skipWaiting
Read documentation on clientsClaim
Read about service worker lifecycle by Jake Archibald, and played around with this demo
Read a bunch of stackoverflow posts, offline cookbook, different blog posts, etc.
self.skipWaiting()
does exactly what you described:
forces the waiting service worker to become the active service
"Active" in this sense does not mean any currently loaded clients are now talking to that service. It instead means that service is now the service to be used whenever a new client requests it.
This is where Clients.claim()
comes in:
When a service worker is initially registered, pages won't use it until they next load.
Without calling claim
, any existing clients will still continue to talk to the older service worker until a full page load.
While most of the time it makes sense to use skipWaiting
and Clients.claim
in conjunction, that is not always the case. If there is a chance of a poor experience for the user due to a service worker not being backwards compatible, Clients.claim
should not be called. Instead, the next time a client is refreshed or loaded, it would now have the new service worker without worry of the breaking change.