Force Service Worker Update on First Visit from User, not Second Visit from User









up vote
2
down vote

favorite












I've recently started to delve into service workers and the related strategies. There seems to be some confusion in my mind, or if I'm understanding them correctly, something of a flaw in the entire API — the issue being that when you make a change to your serviceworker.js file (i.e. change a version number in this file because you've made changes to your website), the user has to then visit the site twice to see the changes? Once to register and install the new service worker, and a second time to activate it (i.e access the new cache).



Now in the real world if there is an issue on your site, whether it be broken link, a javascript error, or the wrong info in a blog post etc, when a user comes back to that site for the first after you've made the fix, there's no way of them seeing this fix unless they come back a second time? Even though you've made the changes?



I mean, this can't be true...surely?



CODE THAT I'M USING



var preCacheList = [
"/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
];

var CACHE_STATIC_NAME = 'static-v1.4';
var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';

self.addEventListener('install', function(event)
console.log('[Service Worker] Installing Service Worker ...', event);
event.waitUntil(
caches.open(CACHE_STATIC_NAME)
.then(function(cache)
console.log('[Service Worker] Precaching App Shell');
cache.addAll(preCacheList);
)
)
);

self.addEventListener('activate', function(event)
console.log('[Service Worker] Activating Service Worker ....', event);
event.waitUntil(
caches.keys()
.then(function(keyList)
return Promise.all(keyList.map(function(key)
if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
console.log('[Service Worker] Removing old cache.', key);
return caches.delete(key);

));
)
);
return self.clients.claim();
);

self.addEventListener('fetch', function(event)
event.respondWith(
caches.match(event.request)
.then(function(response)
if (response)
return response;
else
return fetch(event.request)
.then(function(res)
return caches.open(CACHE_DYNAMIC_NAME)
.then(function(cache)
cache.put(event.request.url, res.clone());
return res;
)
)
.catch(function(err)

);

)
);
);









share|improve this question

























    up vote
    2
    down vote

    favorite












    I've recently started to delve into service workers and the related strategies. There seems to be some confusion in my mind, or if I'm understanding them correctly, something of a flaw in the entire API — the issue being that when you make a change to your serviceworker.js file (i.e. change a version number in this file because you've made changes to your website), the user has to then visit the site twice to see the changes? Once to register and install the new service worker, and a second time to activate it (i.e access the new cache).



    Now in the real world if there is an issue on your site, whether it be broken link, a javascript error, or the wrong info in a blog post etc, when a user comes back to that site for the first after you've made the fix, there's no way of them seeing this fix unless they come back a second time? Even though you've made the changes?



    I mean, this can't be true...surely?



    CODE THAT I'M USING



    var preCacheList = [
    "/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
    ];

    var CACHE_STATIC_NAME = 'static-v1.4';
    var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';

    self.addEventListener('install', function(event)
    console.log('[Service Worker] Installing Service Worker ...', event);
    event.waitUntil(
    caches.open(CACHE_STATIC_NAME)
    .then(function(cache)
    console.log('[Service Worker] Precaching App Shell');
    cache.addAll(preCacheList);
    )
    )
    );

    self.addEventListener('activate', function(event)
    console.log('[Service Worker] Activating Service Worker ....', event);
    event.waitUntil(
    caches.keys()
    .then(function(keyList)
    return Promise.all(keyList.map(function(key)
    if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
    console.log('[Service Worker] Removing old cache.', key);
    return caches.delete(key);

    ));
    )
    );
    return self.clients.claim();
    );

    self.addEventListener('fetch', function(event)
    event.respondWith(
    caches.match(event.request)
    .then(function(response)
    if (response)
    return response;
    else
    return fetch(event.request)
    .then(function(res)
    return caches.open(CACHE_DYNAMIC_NAME)
    .then(function(cache)
    cache.put(event.request.url, res.clone());
    return res;
    )
    )
    .catch(function(err)

    );

    )
    );
    );









    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I've recently started to delve into service workers and the related strategies. There seems to be some confusion in my mind, or if I'm understanding them correctly, something of a flaw in the entire API — the issue being that when you make a change to your serviceworker.js file (i.e. change a version number in this file because you've made changes to your website), the user has to then visit the site twice to see the changes? Once to register and install the new service worker, and a second time to activate it (i.e access the new cache).



      Now in the real world if there is an issue on your site, whether it be broken link, a javascript error, or the wrong info in a blog post etc, when a user comes back to that site for the first after you've made the fix, there's no way of them seeing this fix unless they come back a second time? Even though you've made the changes?



      I mean, this can't be true...surely?



      CODE THAT I'M USING



      var preCacheList = [
      "/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
      ];

      var CACHE_STATIC_NAME = 'static-v1.4';
      var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';

      self.addEventListener('install', function(event)
      console.log('[Service Worker] Installing Service Worker ...', event);
      event.waitUntil(
      caches.open(CACHE_STATIC_NAME)
      .then(function(cache)
      console.log('[Service Worker] Precaching App Shell');
      cache.addAll(preCacheList);
      )
      )
      );

      self.addEventListener('activate', function(event)
      console.log('[Service Worker] Activating Service Worker ....', event);
      event.waitUntil(
      caches.keys()
      .then(function(keyList)
      return Promise.all(keyList.map(function(key)
      if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
      console.log('[Service Worker] Removing old cache.', key);
      return caches.delete(key);

      ));
      )
      );
      return self.clients.claim();
      );

      self.addEventListener('fetch', function(event)
      event.respondWith(
      caches.match(event.request)
      .then(function(response)
      if (response)
      return response;
      else
      return fetch(event.request)
      .then(function(res)
      return caches.open(CACHE_DYNAMIC_NAME)
      .then(function(cache)
      cache.put(event.request.url, res.clone());
      return res;
      )
      )
      .catch(function(err)

      );

      )
      );
      );









      share|improve this question













      I've recently started to delve into service workers and the related strategies. There seems to be some confusion in my mind, or if I'm understanding them correctly, something of a flaw in the entire API — the issue being that when you make a change to your serviceworker.js file (i.e. change a version number in this file because you've made changes to your website), the user has to then visit the site twice to see the changes? Once to register and install the new service worker, and a second time to activate it (i.e access the new cache).



      Now in the real world if there is an issue on your site, whether it be broken link, a javascript error, or the wrong info in a blog post etc, when a user comes back to that site for the first after you've made the fix, there's no way of them seeing this fix unless they come back a second time? Even though you've made the changes?



      I mean, this can't be true...surely?



      CODE THAT I'M USING



      var preCacheList = [
      "/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
      ];

      var CACHE_STATIC_NAME = 'static-v1.4';
      var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';

      self.addEventListener('install', function(event)
      console.log('[Service Worker] Installing Service Worker ...', event);
      event.waitUntil(
      caches.open(CACHE_STATIC_NAME)
      .then(function(cache)
      console.log('[Service Worker] Precaching App Shell');
      cache.addAll(preCacheList);
      )
      )
      );

      self.addEventListener('activate', function(event)
      console.log('[Service Worker] Activating Service Worker ....', event);
      event.waitUntil(
      caches.keys()
      .then(function(keyList)
      return Promise.all(keyList.map(function(key)
      if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
      console.log('[Service Worker] Removing old cache.', key);
      return caches.delete(key);

      ));
      )
      );
      return self.clients.claim();
      );

      self.addEventListener('fetch', function(event)
      event.respondWith(
      caches.match(event.request)
      .then(function(response)
      if (response)
      return response;
      else
      return fetch(event.request)
      .then(function(res)
      return caches.open(CACHE_DYNAMIC_NAME)
      .then(function(cache)
      cache.put(event.request.url, res.clone());
      return res;
      )
      )
      .catch(function(err)

      );

      )
      );
      );






      javascript html caching service-worker






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 19:18









      The Chewy

      583521




      583521






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          I found the solution to this — you need to add the skipWaiting() method in the initial installation event.



          I've re-posted the code from my question above with a comment on this line:



          var preCacheList = [
          "/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
          ];

          var CACHE_STATIC_NAME = 'static-v1.4';
          var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';

          self.addEventListener('install', function(event)
          console.log('[Service Worker] Installing Service Worker ...', event);
          event.waitUntil(
          caches.open(CACHE_STATIC_NAME)
          .then(function(cache)
          console.log('[Service Worker] Precaching App Shell');
          cache.addAll(preCacheList);
          )
          )
          return self.skipWaiting(); // THIS IS THE LINE THAT OVER-RIDES THE SECOND RELOAD.

          );

          self.addEventListener('activate', function(event)
          console.log('[Service Worker] Activating Service Worker ....', event);
          event.waitUntil(
          caches.keys()
          .then(function(keyList)
          return Promise.all(keyList.map(function(key)
          if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
          console.log('[Service Worker] Removing old cache.', key);
          return caches.delete(key);

          ));
          )
          );
          return self.clients.claim();
          );

          self.addEventListener('fetch', function(event)
          event.respondWith(
          caches.match(event.request)
          .then(function(response)
          if (response)
          return response;
          else
          return fetch(event.request)
          .then(function(res)
          return caches.open(CACHE_DYNAMIC_NAME)
          .then(function(cache)
          cache.put(event.request.url, res.clone());
          return res;
          )
          )
          .catch(function(err)

          );

          )
          );
          );





          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232039%2fforce-service-worker-update-on-first-visit-from-user-not-second-visit-from-user%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            I found the solution to this — you need to add the skipWaiting() method in the initial installation event.



            I've re-posted the code from my question above with a comment on this line:



            var preCacheList = [
            "/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
            ];

            var CACHE_STATIC_NAME = 'static-v1.4';
            var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';

            self.addEventListener('install', function(event)
            console.log('[Service Worker] Installing Service Worker ...', event);
            event.waitUntil(
            caches.open(CACHE_STATIC_NAME)
            .then(function(cache)
            console.log('[Service Worker] Precaching App Shell');
            cache.addAll(preCacheList);
            )
            )
            return self.skipWaiting(); // THIS IS THE LINE THAT OVER-RIDES THE SECOND RELOAD.

            );

            self.addEventListener('activate', function(event)
            console.log('[Service Worker] Activating Service Worker ....', event);
            event.waitUntil(
            caches.keys()
            .then(function(keyList)
            return Promise.all(keyList.map(function(key)
            if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
            console.log('[Service Worker] Removing old cache.', key);
            return caches.delete(key);

            ));
            )
            );
            return self.clients.claim();
            );

            self.addEventListener('fetch', function(event)
            event.respondWith(
            caches.match(event.request)
            .then(function(response)
            if (response)
            return response;
            else
            return fetch(event.request)
            .then(function(res)
            return caches.open(CACHE_DYNAMIC_NAME)
            .then(function(cache)
            cache.put(event.request.url, res.clone());
            return res;
            )
            )
            .catch(function(err)

            );

            )
            );
            );





            share|improve this answer
























              up vote
              0
              down vote



              accepted










              I found the solution to this — you need to add the skipWaiting() method in the initial installation event.



              I've re-posted the code from my question above with a comment on this line:



              var preCacheList = [
              "/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
              ];

              var CACHE_STATIC_NAME = 'static-v1.4';
              var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';

              self.addEventListener('install', function(event)
              console.log('[Service Worker] Installing Service Worker ...', event);
              event.waitUntil(
              caches.open(CACHE_STATIC_NAME)
              .then(function(cache)
              console.log('[Service Worker] Precaching App Shell');
              cache.addAll(preCacheList);
              )
              )
              return self.skipWaiting(); // THIS IS THE LINE THAT OVER-RIDES THE SECOND RELOAD.

              );

              self.addEventListener('activate', function(event)
              console.log('[Service Worker] Activating Service Worker ....', event);
              event.waitUntil(
              caches.keys()
              .then(function(keyList)
              return Promise.all(keyList.map(function(key)
              if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
              console.log('[Service Worker] Removing old cache.', key);
              return caches.delete(key);

              ));
              )
              );
              return self.clients.claim();
              );

              self.addEventListener('fetch', function(event)
              event.respondWith(
              caches.match(event.request)
              .then(function(response)
              if (response)
              return response;
              else
              return fetch(event.request)
              .then(function(res)
              return caches.open(CACHE_DYNAMIC_NAME)
              .then(function(cache)
              cache.put(event.request.url, res.clone());
              return res;
              )
              )
              .catch(function(err)

              );

              )
              );
              );





              share|improve this answer






















                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                I found the solution to this — you need to add the skipWaiting() method in the initial installation event.



                I've re-posted the code from my question above with a comment on this line:



                var preCacheList = [
                "/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
                ];

                var CACHE_STATIC_NAME = 'static-v1.4';
                var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';

                self.addEventListener('install', function(event)
                console.log('[Service Worker] Installing Service Worker ...', event);
                event.waitUntil(
                caches.open(CACHE_STATIC_NAME)
                .then(function(cache)
                console.log('[Service Worker] Precaching App Shell');
                cache.addAll(preCacheList);
                )
                )
                return self.skipWaiting(); // THIS IS THE LINE THAT OVER-RIDES THE SECOND RELOAD.

                );

                self.addEventListener('activate', function(event)
                console.log('[Service Worker] Activating Service Worker ....', event);
                event.waitUntil(
                caches.keys()
                .then(function(keyList)
                return Promise.all(keyList.map(function(key)
                if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
                console.log('[Service Worker] Removing old cache.', key);
                return caches.delete(key);

                ));
                )
                );
                return self.clients.claim();
                );

                self.addEventListener('fetch', function(event)
                event.respondWith(
                caches.match(event.request)
                .then(function(response)
                if (response)
                return response;
                else
                return fetch(event.request)
                .then(function(res)
                return caches.open(CACHE_DYNAMIC_NAME)
                .then(function(cache)
                cache.put(event.request.url, res.clone());
                return res;
                )
                )
                .catch(function(err)

                );

                )
                );
                );





                share|improve this answer












                I found the solution to this — you need to add the skipWaiting() method in the initial installation event.



                I've re-posted the code from my question above with a comment on this line:



                var preCacheList = [
                "/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
                ];

                var CACHE_STATIC_NAME = 'static-v1.4';
                var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';

                self.addEventListener('install', function(event)
                console.log('[Service Worker] Installing Service Worker ...', event);
                event.waitUntil(
                caches.open(CACHE_STATIC_NAME)
                .then(function(cache)
                console.log('[Service Worker] Precaching App Shell');
                cache.addAll(preCacheList);
                )
                )
                return self.skipWaiting(); // THIS IS THE LINE THAT OVER-RIDES THE SECOND RELOAD.

                );

                self.addEventListener('activate', function(event)
                console.log('[Service Worker] Activating Service Worker ....', event);
                event.waitUntil(
                caches.keys()
                .then(function(keyList)
                return Promise.all(keyList.map(function(key)
                if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
                console.log('[Service Worker] Removing old cache.', key);
                return caches.delete(key);

                ));
                )
                );
                return self.clients.claim();
                );

                self.addEventListener('fetch', function(event)
                event.respondWith(
                caches.match(event.request)
                .then(function(response)
                if (response)
                return response;
                else
                return fetch(event.request)
                .then(function(res)
                return caches.open(CACHE_DYNAMIC_NAME)
                .then(function(cache)
                cache.put(event.request.url, res.clone());
                return res;
                )
                )
                .catch(function(err)

                );

                )
                );
                );






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 16:04









                The Chewy

                583521




                583521



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232039%2fforce-service-worker-update-on-first-visit-from-user-not-second-visit-from-user%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Barbados

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3