<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nishant Contractor</title>
    <description>The latest articles on DEV Community by Nishant Contractor (@niscontractor).</description>
    <link>https://dev.to/niscontractor</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F319221%2F8e18871c-55e8-4f91-bddf-aeb8d9bc7297.jpg</url>
      <title>DEV Community: Nishant Contractor</title>
      <link>https://dev.to/niscontractor</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/niscontractor"/>
    <language>en</language>
    <item>
      <title>Everything you want to know about Service Worker</title>
      <dc:creator>Nishant Contractor</dc:creator>
      <pubDate>Fri, 28 Feb 2020 10:51:57 +0000</pubDate>
      <link>https://dev.to/niscontractor/everything-you-want-to-know-about-service-worker-4c91</link>
      <guid>https://dev.to/niscontractor/everything-you-want-to-know-about-service-worker-4c91</guid>
      <description>&lt;p&gt;This blog is all about service worker, how its work, where we can use it, how we can remove that, where it will not work and many more. Will start with the introduction. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Service Worker&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Service worker is java script file that runs in user’s browser as background process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How service worker works&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;It intercept network requests, caching or retrieving resources from the cache, and delivering push messages.Service workers can be accessed by many tabs in the browser at the same time because only one service worker can Exists per scope and existence is independent from the main thread.&lt;/p&gt;

&lt;p&gt;To install Service worker in your project, first you need to register service worker&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Register service worker&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To register service worker first we need to check if browser supports it or not? using “navigator Object”.navigator Object contains information about the browser.&lt;/p&gt;

&lt;p&gt;Your first service worker downloads when you call register() function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;serviceWorker&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serviceWorker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/ServiceWorker.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Service worker registration done&lt;/span&gt;
       &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Registration Successful&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;// Service worker registration failed&lt;/span&gt;
       &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Registration Failed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The service worker can only access folders below it, so it is probably best to keep it in the root of the project (the top most folder).&lt;br&gt;
Register function on service worker downloads the service worker script from provided URL and executes it.&lt;/p&gt;

&lt;p&gt;In above code response contains information about the state of the service worker as well as it’s scope.&lt;/p&gt;

&lt;p&gt;In ServiceWorker.js file we can write code for installation,update, push etc.&lt;/p&gt;

&lt;p&gt;If service worker fails to register, the register promise rejects, and the service worker is discarded.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to add service worker&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After Successfully registration of service worker, installation can executes:&lt;/p&gt;

&lt;p&gt;Install Service Worker:&lt;/p&gt;

&lt;p&gt;If service worker is the new or updated, the installation process will start.At this stage you can cached you static content or file.&lt;br&gt;
This event only trigger if “ServiceWorker.js” doesn’t exists or file is newer version(updated).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;install&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cach&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;waitUntil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
     &lt;span class="nx"&gt;caches&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;v1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addAll&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
           &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/favicon.ico&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="p"&gt;]);&lt;/span&gt;
       &lt;span class="p"&gt;})&lt;/span&gt;
     &lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Service Worker not installed if any file failed to catched which is provide in cache.addAll() function.&lt;br&gt;
By using event.waitUntil() service worker not terminate until the promise passed to waitUntil() method is either resolved or rejected.&lt;/p&gt;

&lt;p&gt;After successful installation Activation event trigger of service worker&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;activate&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;v1 now ready to handle fetches&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="p"&gt;})&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How to clear service worker cache&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In activation event we can remove or delete existed cached item using cache.delete() method.&lt;/p&gt;

&lt;p&gt;For Remove Clear Catch&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;activate&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;waitUntil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="nx"&gt;caches&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cacheNames&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;cacheNames&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cacheName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;caches&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cacheName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;});&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;
 &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;caches.keys() method provide names od all caches accessible to you.&lt;/p&gt;

&lt;p&gt;Everytime user makes request for page, service workers fetch event fires.&lt;br&gt;
In this event we decide whether we want that request to go to the network or present catch items.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fetch&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="c1"&gt;//serve the image from catch if the request is same origin&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//image which is stored at the time of installation&lt;/span&gt;
    &lt;span class="c1"&gt;//respond with cached &lt;/span&gt;
       &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;respondWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;caches&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/favicon.ico&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Give respond with netweork call&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fetch&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;respondWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
 &lt;span class="p"&gt;})&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;respondWith() method allows you to promise for a Response yourself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update Service Worker&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we make changes in the service worker’s script file then the browser is considered as a new service worker and it’s installed event fire.&lt;/p&gt;

&lt;p&gt;When we update service worker, new service worker not control clients because &lt;br&gt;
Old service workers handle client’s. When we close browser the old service will be killed and new one takes place of old service worker.&lt;/p&gt;

&lt;p&gt;If we want to use a new service worker as soon as installation is completed we can use the self.skipWaiting() method.&lt;br&gt;
This method activates service workers as it’s finished installing.&lt;/p&gt;

&lt;p&gt;If a new service worker fails to install, the old one continues to handle clients(pages).&lt;/p&gt;

&lt;p&gt;In development mode Check the checkbox update on reload which is update service worker on page refresh.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Debugging Service Worker&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Service workers are only available to "secure origins" (HTTPS sites, basically). &lt;/p&gt;

&lt;p&gt;To Inspect service worker from chrome&lt;br&gt;
Goto to developer tool(F12) or right click on page click on inspect tab&lt;br&gt;
Go to application tab and then click on service worker from sidebar&lt;br&gt;
In Sidebar you have list of register service Worker.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to test service worker on localhost&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For developing purposes You can use localhost because localhost is also considered as “secure origins”. Or setup a local server by using npm http-server and serve package.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unregister Service Worker/ Remove service worker / Disable service worker&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For unregister service worker&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;serviceWorker&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serviceWorker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getRegistration&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
       &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;registrations&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;registration&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;registrations&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="nx"&gt;registration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unregister&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
     &lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The service worker may not die immediately. If the worker is currently performing any tasks for a client it will complete before being officially removed.It will only be removed after page reload or closed.&lt;/p&gt;

&lt;p&gt;Because of this you can make dummy new service workers and use it in place of currently working service workers.&lt;br&gt;
By Placing unregister() method of service worker in activation event of service worker and force each client’s browser to reload the page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How To manually unregistered service worker from chrome&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Goto to developer tool(F12) or right click on page click on inspect tab&lt;br&gt;
Go to application tab and then click on service worker from sidebar&lt;br&gt;
    In service worker you have list of registered service worker &lt;br&gt;
    From that you can manually unregister service worker by clicking on the unregister link.&lt;/p&gt;

</description>
      <category>serviceworker</category>
      <category>question</category>
      <category>caching</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Most frequently asked questions in PWA</title>
      <dc:creator>Nishant Contractor</dc:creator>
      <pubDate>Thu, 06 Feb 2020 12:04:47 +0000</pubDate>
      <link>https://dev.to/niscontractor/most-frequently-asked-questions-in-pwa-4mg9</link>
      <guid>https://dev.to/niscontractor/most-frequently-asked-questions-in-pwa-4mg9</guid>
      <description>&lt;p&gt;I am writing this blog to share my findings on a question that I had during the developments of PWA. I went through many blogs and tutorials for this and I found few things are not working with IOS while Android has good support for PWA. &lt;/p&gt;

&lt;p&gt;Let's see them one by one. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can PWA access camera?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yes, both the OS gives access to the camera, which allows image and video capturing in PWA. &lt;br&gt;
Android: YES&lt;br&gt;
IOS: YES&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can PWA work offline?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PWA leverages data caching during your last interactions with the app, and by doing that it works offline while there is no internet connection. &lt;br&gt;
Android: YES&lt;br&gt;
IOS: YES&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can PWA access the file system?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt; is the only way you can access a regular storage file system, which will not have any event listener. PWA can't listen to regular file system changes except for the files of APP. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can PWA access the Bluetooth?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The web Bluetooth API is a low-level API allowing web applications to pair with the nearby Bluetooth Low Energy-enabled peripheral devices and access their services exposed. But it's not supported on all platforms. &lt;br&gt;
Android: YES&lt;br&gt;
IOS: NO&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can PWA access the phone’s GPS?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A lot of browsers support this recent feature and these include Chrome, Edge, Firefox, Safari, and Opera. In IE, you’ll have to battle a few configurations to make it work &lt;br&gt;
Android: YES&lt;br&gt;
IOS: YES&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can PWA use advanced camera functionality?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Advanced camera functionality is not available on all platforms. For example QR CODE scanning.&lt;br&gt;&lt;br&gt;
Android: YES&lt;br&gt;
IOS: NO&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can PWA access contacts?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;No there is no way to access contacts in PWA, it is mainly restricted for security purposes. &lt;br&gt;
Android: NO&lt;br&gt;
IOS: NO&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can PWA check for new updates?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yes, in order to achieve this you have to check your cached files are changed or not. We can implement this by writing a hook in onupdatefound function on the registered Service Worker.&lt;/p&gt;

&lt;p&gt;Here I found a detailed blog for this that explained each step in detail. &lt;br&gt;
&lt;a href="https://medium.com/progressive-web-apps/pwa-create-a-new-update-available-notification-using-service-workers-18be9168d717"&gt;https://medium.com/progressive-web-apps/pwa-create-a-new-update-available-notification-using-service-workers-18be9168d717&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can PWA have Push Notifications?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the most crucial part of any applications and many product owners avoid PWA just because of these limitations. Android support push notifications but IOS does not support. Maybe Apple can update in the next version 14. But we have to wait till that. &lt;br&gt;
Android: YES&lt;br&gt;
IOS: NO&lt;/p&gt;

</description>
      <category>pwa</category>
      <category>issues</category>
      <category>ios</category>
      <category>angular</category>
    </item>
    <item>
      <title>QR Code integration in PWA and its challenges </title>
      <dc:creator>Nishant Contractor</dc:creator>
      <pubDate>Thu, 30 Jan 2020 09:45:41 +0000</pubDate>
      <link>https://dev.to/niscontractor/qr-code-integration-in-pwa-and-its-challenges-18o4</link>
      <guid>https://dev.to/niscontractor/qr-code-integration-in-pwa-and-its-challenges-18o4</guid>
      <description>&lt;p&gt;     We were developing a PWA app that needs us to scan QR codes if user in order to validate them. Now we knew how to integrate QR scanner in javascript but it was our first time PWA and we were clueless how we would achieve that in PWA. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Our Need?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;     We needed to implement QR code scanner functionality to confirm that users met with each other. Where one user shows QR code in their app and the other user needs to scan that QR code from the app. &lt;/p&gt;

&lt;p&gt;     To implement this we needed two things &lt;br&gt;
          QR CODE Generator (Which encode data)&lt;br&gt;
          QR CODE Scanner (Which decode data)&lt;/p&gt;

&lt;p&gt;     We went through the multiple libraries and amongst them, we used the following libraries. &lt;br&gt;
          &lt;a href="https://www.npmjs.com/package/angularx-qrcode" rel="noopener noreferrer"&gt;angularx-qrcode&lt;/a&gt; to generate QR Code.&lt;br&gt;
          &lt;a href="https://github.com/zxing-js/ngx-scanner" rel="noopener noreferrer"&gt;ngx-scanner&lt;/a&gt; to scan generated QR Code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; Many times the scanner library does not scan QRs of all libraries. So you need to select a library accordingly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where we got stuck?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;     Once we got the requirement we decided to make POC for that. We followed the steps given in the library documentation and created a nice demo where we generate QR code with required encrypted data which was decode by the integrated QR scanner. We tested this after adding an app to the home screen and it worked.&lt;/p&gt;

&lt;p&gt;     Unfortunately we tested this in Android device and not in an IOS device. We knew IOS PWA supports camera features but were unaware that it does not support streaming in camera. Hence the QR scanning did not work in IOS PWA. &lt;/p&gt;

&lt;p&gt;     We had committed to deliver this feature to our client as we had tested the POC&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Our workaround.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;     We were able to catch and handle errors generated while the user tried to scan QR code in and IOS device. So we decided to handle this by checking QR supportiveness of the devices before the user reached that screen. If the user is on an unsupported device we would not show the user scan button, instead of that we show the user an OTP screen that they can ask to other users. The OTP screen for other users has a toggle switch between the QR code and the OTP screen to manage the flow accordingly.   &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftrt-react-images.s3-us-west-2.amazonaws.com%2FiphonePWD.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftrt-react-images.s3-us-west-2.amazonaws.com%2FiphonePWD.jpg" alt="PWA QR CODE"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How should you start?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;     1) First understand the requirements clearly. Make small  POC or demo before committing anything in PWA as there are too many things that are still not supported in IOS devices.&lt;br&gt;
     2) Find out a workaround that fulfils the system requirement without bothering users.&lt;br&gt;
     3) Select libraries whose community is more active and supports the latest version of the technologies being used.&lt;/p&gt;

</description>
      <category>pwa</category>
      <category>qrcode</category>
      <category>challenge</category>
      <category>angular</category>
    </item>
    <item>
      <title>Challenges with IOS PWA
</title>
      <dc:creator>Nishant Contractor</dc:creator>
      <pubDate>Fri, 24 Jan 2020 12:21:05 +0000</pubDate>
      <link>https://dev.to/niscontractor/challenges-with-ios-pwa-13ho</link>
      <guid>https://dev.to/niscontractor/challenges-with-ios-pwa-13ho</guid>
      <description>&lt;p&gt;We are writing this blog after spending two thousand hours in PWA development. All PWA brings different challenges and we are sharing our experience here. We have faced a lot of issues while developing PWA applications for our clients from which many issues we had to resolve with other alternative approaches. &lt;/p&gt;

&lt;p&gt;PWA has the ability to work on multiple platforms with the behavior of native app. This blog is more about PWA with the IOS platform as Apple is still lagging compared to other platforms. We will be sharing the challenges we faced while developing a dating app. &lt;/p&gt;

&lt;p&gt;How we started the development of PWA for a dating application? &lt;/p&gt;

&lt;p&gt;Our native mobile application team created a flawless dating app with all the required features which a dating app needed. All the development was done and it went through all the phases of testing. The client was very happy with the performance and end result of the app. And it became a sad day for him when Apple’s review team rejected the application due to nontechnical reasons. The client was confident about the concept and anyhow wanted to take the application to the market. Hence, we started the development of the same app as PWA.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Challenge 1: Social Login&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Social Login is the first step in the dating app where we integrated Google login. Our PWA could not work with Google plugin as its open google login screen in the new browser tab and its redirect user on redirect URL in the same browser tab and not in the PWA. &lt;/p&gt;

&lt;p&gt;To fix this issue we wrote our own code where we create a child window in PWA and open google’s login screen inside it. Once a user is authorized we took that data from google and closed the child window. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;Challenge 2: QR Scanning&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Our application has two user types of male and female and before they start the date, both users need to verify their presence at the decided place. And for that, we decided to use QR code where one user needs to scan other user’s code to confirm both users meet each other at the decided place. The camera feature is available in IOS PWA but unfortunately, the camera stream is not supported. &lt;/p&gt;

&lt;p&gt;As an alternative we have implemented OTP based functionality. First we check whether the device supported camera stream or not? If it's allowed then we render the QR scanner and if the camera stream is not allowed in the device we render the OTP screen. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;Challenge 3: Location (GPS) &lt;/b&gt;&lt;/p&gt;

&lt;p&gt;In a dating app user location is the most important feature to show the nearest match. PWA has access to location service on devices and for that it will ask user permission. Everything works well if the PWA is allowed to access location service, issues will happen when the PWA is not allowed to use that service and as there is no other way to ask again to the user for location service access we got stuck. &lt;/p&gt;

&lt;p&gt;To overcome this issue we have integrated third party API which give user location based on IP address. So first our PWA asks the user to allow permission for location service if the user allows then we read the user’s geolocation details from the device and if the user denied the permission then we use the 3rd party API to get the user’s geolocation. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;Challenge 4: Push notification&lt;/b&gt;&lt;br&gt;
Push notification is a very important functionality for any app. We need to use push notification to support chat functionality and it was also used to inform other users on each and every status change of their date like date requested, date accepted, date rejected etc. &lt;/p&gt;

&lt;p&gt;Push notification is not working with IOS PWA as IOS is not supporting background processing. &lt;br&gt;
So first we track the user device information in our system and we have implemented triggers for push notification that send notifications to android users and for IOS users we have written SMS triggers. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;Other things&lt;/b&gt;&lt;br&gt;
Few other normally overseen things that needs to be considered while developing a PWA that supports IOS devices.&lt;br&gt;
IOS allows only 50 MB to PWA, so it's very important to plan app caching logic accordingly. &lt;br&gt;
Background sync is also not allowed in IOS for PWAs, which a major drawback for any application. &lt;br&gt;
Apple removed unused items (files) from disk if the PWA app is not used more than 15days(approx). &lt;/p&gt;

&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;For Progressive Web Apps on iOS, there are certain limitations, but they aren't deal-breakers. Many of the features most requested have some form of a fallback solution, at least. It may not provide the native Web platform API or service offerings with comparable user experience.&lt;/p&gt;

&lt;p&gt;For most mobile apps, particularly on iOS, there isn't a good channel for promoting and engaging your clients. For most applications, app installs are rare. The creation, management and promotion are also very costly for these applications.&lt;/p&gt;

&lt;p&gt;Specifically, the web progressive web apps, are available on every device in every browser. And they can be sold through online channels, PPC and traditional marketing funnels at an affordable price.&lt;/p&gt;

&lt;p&gt;Many brands, especially on iOS, have reported improved customer engagement stats after upgrading their websites to a PWA. Your brand will probably do so too&lt;/p&gt;

</description>
      <category>pwa</category>
      <category>ios</category>
      <category>challenge</category>
      <category>angular</category>
    </item>
  </channel>
</rss>
