python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 7, Interview Questions Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
Service Worker Life Cycle

What Is Service Worker Life Cycle?

Service Worker Life Cycle - A service worker has a life cycle that is completely separate from your web apps page.
To install a service worker for our site, we need to register it, which we do in our pages. To Registering a service worker will cause the browser to start the service worker install step in the background process.

Prerequisites to Supports Service workers-
ü  Browser support
ü  You need HTTPS

How To Register A Service Worker?
To install a service worker you need to kick start the process by registering it in your page. This tells the browser where your service worker JavaScript file lives.

You can call below register () every time a page loads without concern; the browser will figure out if the service worker is already registered or not and handle it accordingly.

if ('serviceWorker' in navigator) {
  window.addEventListener('load', function() {
    navigator.serviceWorker.register('/sw.js').then(function(registration) {
      // If Registration was successful
      console.log('Success Registration - ', registration.scope);
    },
    function(err) {
      // If Registration was failed!
      console.log('Failed Registration - ', err);
    });
  });
}


This code checks to see if the service worker API is available, and if it is, the service worker at /sw.js is registered once the page is loaded.

I hope you are enjoying with this post! Please share with you friends!! Thank you!!!
ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions