Skip to main content

Posts

Showing posts with the label Angular Service Worker Life Cycle

Angular Service Worker Cache and return Requests

How to Cache and return Requests? After a service worker is installed and the user navigates to a different page or refreshes, the service worker will begin to receive fetch events. Got a minute? Check out this,  Angular 6 Questions | A Complete Guide Book The following example looks like this. self . addEventListener ( 'fetch' , function ( event ) {   event . respondWith (     caches . match ( event . request )       . then ( function ( response ) {         //Cache return response         if ( response ) {            return response ;         }                return fetch ( event . request );       })   ); }); What Is Angular Language Service? Th...

Install a Service Worker in Angular

How to Install a Service Worker? After a controlled page kicks off the registration process, let's shift to the point of view of the service worker script, which handles the install event. The following example looks like this. self . addEventListener ( 'install' , function ( event ) {   // To perform install steps }); Inside of our install callback, we need to take the following steps - 1.       Open a cache 2.       Cache our files 3.       Confirm whether all the required assets are cached or not The following example looks like this. var CACHE_NAME = 'my-site-cache-v1.0' ; var urlsToCache = [ '/' , '/styles/site.css' , '/script/site.js' ]; self . addEventListener ( 'install' , function ( event ) {   // Perform install steps   event . waitUntil (     caches . open ( CACHE_NAME )       . then ( function ( cache...

Register a Service Worker in Angular

How to Register a Service Worker? To install a service worker you need to kick-start the process by registering it on 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!      ...

Angular Service Worker Life Cycle

What Is 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 on 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- 1.        Browser support 2.        You need HTTPS Got a minute? Check out this, Angular 6 Questions | A Complete Guide Book