

What self.skipWaiting() does to the service worker lifecycle
source link: https://www.tuicool.com/articles/hit/rQJ73uB
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The ServiceWorker.skipWaiting()
method is a life saver. It ensures that any new versions of a service worker will take over the page and become activated immediately.
To understand why this is so important and useful, we need to revisit the service worker lifecycle. I have a more detailed article about each step of the lifecycle, but you just need to know that there are six states that a service worker can be in - parsed, installing, installed (waiting), activating, activated, and redundant.
When we first try to register a service worker via navigator.serviceWorker.register()
, the file passed is parsed and, assuming there are no errors, the service worker is installed. For a page with no previous service worker files, the newly installed service worker becomes activated immediately.
If, however, the page already has an activated service worker file, things are more complicated.
Endless waiting
If the page already has an activated service worker and a new file is pushed, the new file will still be parsed and installed. Once installed, it will wait for an opportunity to become activated.
Without self.skipWaiting()
, a waiting service worker will only become active itself once the currently active service worker is released and becomes redundant. This can only happen in two scenarios:
- If the user has navigated away from the page, thereby releasing the previous active worker
- If a specified period of time has passed, thereby releasing the previous active worker
So, unlike we are used to when pushing new versions of assets to our website, a new version of a service worker can be waiting for what seems like forever. Even if a user refreshes their page, they may not receive the updated version for a very long time.
self.skipWaiting()
to the rescue!
The ServiceWorker.skipWaiting()
method solves this problem by telling the newly installed service worker to skip the waiting state and move directly to activating.
The self.skipWaiting()
method is typically used in the install
event of the service worker. As long as the method is called before the waiting phase itself, the service worker will skip the waiting phase and become immediately activated.
service-worker.js
self.addEventListener('install', function (event) { self.skipWaiting(); event.waitUntil( // Do stuff ); });
Recommend
-
16
Introducing Swift Service Lifecycle July 15, 2020 Tom Doron Tom Doron is a member of the Swift Core Team and the Swift Server...
-
3
The Solution to Service Lifecycle Automation may be Process Automation How much change in software is required for “digital transformation?” Do you need to redo everything to cloud-native form? There’s a school of thought, now repres...
-
7
Evolving Principles for Service and Application Lifecycle Modeling and Automation Applications aren’t possible without application development, and in today’s hosted-feature age, neither are advanced services. That makes the question...
-
11
The primary focus of the 2020 State of DevOps report is the emergence of a self-service developer platform that top-performing DevOps teams are adopting. The reason fo...
-
9
Does OSS/BSS Modernization Figure into Modernized Lifecycle Automation? A unified lifecycle automation model, as I noted in my blog on March 15th, seems to be the only way for operators to stabilize or reduce opex, now th...
-
8
Looking for the “Why” in Service Lifecycle Automation What is the goal, the real and ultimate goal, of service lifecycle automation? That may seem like a stupid question, and in more than one dimension. First-off, everyon...
-
4
What is Lifecycle Manager in AWS EC2 Service In this article, we will see how to use Lifecycle Manager to automate the creation, retention, and deletion of snapshots taken to back up our Amazon EBS volumes. This automation helps us to pr...
-
11
Some Top-Down Service Lifecycle Modeling and Orchestration Commentary Most of you know that I’ve been an advocate of intent-modeled, model-driven, networking for almost two decades. This approach would divide a...
-
9
Where Does the MEF’s LSO Model Fit in Lifecycle Automation? One of my favorite authors on networking in the last century (it sure feels odd writing that!) was Tannenbaum, and I loved his quote in “Computer Netwo...
-
9
Infrastructure Twitter’s Blobstore Hardware Lifecycle Monitoring and Reporting Service
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK