Using a service worker to get list of files to cache from server











up vote
0
down vote

favorite












I´m trying to use a service worker in an existing asp mvc app. Generally, it´s working fine: I can cache files and so on. Problem is, that there are many files to be cached and I´m trying to return an array of paths to the service worker, so that the files can be added to cache without adding them manually.



Here´s what I have so far:



Controller:



    public ActionResult GetFilesToCache()
{
string filePaths = Directory.GetFiles(Server.MapPath(@"~Content"), "*", SearchOption.AllDirectories);
string cuttedFiles = new string[filePaths.Length];
int i = 0;

foreach (var path in filePaths)
{
cuttedFiles[i] = path.Substring(path.IndexOf("Content"));
i++;
}

return Json(new { filesToCache = cuttedFiles }, JsonRequestBehavior.AllowGet);
}


This gives me a string array with entries like "Contentimage1.png" etc.



Service worker:



    self.addEventListener('install', function(e) {
console.log('[ServiceWorker] Install');
e.waitUntil(
caches.open(cacheName).then(function (cache) {
console.log('[ServiceWorker] Caching app shell');

return fetch('Home/GetFilesToCache').then(function (response) {
return response;
}).then(function (files) {
return cache.addAll(files);
});
})
);
});


The error I get is:



Uncaught (in promise) TypeError: Failed to execute 'addAll' on 'Cache': Iterator getter is not callable.


Calling the action works just fine, data is received by the service worker, but not added to cache.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I´m trying to use a service worker in an existing asp mvc app. Generally, it´s working fine: I can cache files and so on. Problem is, that there are many files to be cached and I´m trying to return an array of paths to the service worker, so that the files can be added to cache without adding them manually.



    Here´s what I have so far:



    Controller:



        public ActionResult GetFilesToCache()
    {
    string filePaths = Directory.GetFiles(Server.MapPath(@"~Content"), "*", SearchOption.AllDirectories);
    string cuttedFiles = new string[filePaths.Length];
    int i = 0;

    foreach (var path in filePaths)
    {
    cuttedFiles[i] = path.Substring(path.IndexOf("Content"));
    i++;
    }

    return Json(new { filesToCache = cuttedFiles }, JsonRequestBehavior.AllowGet);
    }


    This gives me a string array with entries like "Contentimage1.png" etc.



    Service worker:



        self.addEventListener('install', function(e) {
    console.log('[ServiceWorker] Install');
    e.waitUntil(
    caches.open(cacheName).then(function (cache) {
    console.log('[ServiceWorker] Caching app shell');

    return fetch('Home/GetFilesToCache').then(function (response) {
    return response;
    }).then(function (files) {
    return cache.addAll(files);
    });
    })
    );
    });


    The error I get is:



    Uncaught (in promise) TypeError: Failed to execute 'addAll' on 'Cache': Iterator getter is not callable.


    Calling the action works just fine, data is received by the service worker, but not added to cache.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I´m trying to use a service worker in an existing asp mvc app. Generally, it´s working fine: I can cache files and so on. Problem is, that there are many files to be cached and I´m trying to return an array of paths to the service worker, so that the files can be added to cache without adding them manually.



      Here´s what I have so far:



      Controller:



          public ActionResult GetFilesToCache()
      {
      string filePaths = Directory.GetFiles(Server.MapPath(@"~Content"), "*", SearchOption.AllDirectories);
      string cuttedFiles = new string[filePaths.Length];
      int i = 0;

      foreach (var path in filePaths)
      {
      cuttedFiles[i] = path.Substring(path.IndexOf("Content"));
      i++;
      }

      return Json(new { filesToCache = cuttedFiles }, JsonRequestBehavior.AllowGet);
      }


      This gives me a string array with entries like "Contentimage1.png" etc.



      Service worker:



          self.addEventListener('install', function(e) {
      console.log('[ServiceWorker] Install');
      e.waitUntil(
      caches.open(cacheName).then(function (cache) {
      console.log('[ServiceWorker] Caching app shell');

      return fetch('Home/GetFilesToCache').then(function (response) {
      return response;
      }).then(function (files) {
      return cache.addAll(files);
      });
      })
      );
      });


      The error I get is:



      Uncaught (in promise) TypeError: Failed to execute 'addAll' on 'Cache': Iterator getter is not callable.


      Calling the action works just fine, data is received by the service worker, but not added to cache.










      share|improve this question















      I´m trying to use a service worker in an existing asp mvc app. Generally, it´s working fine: I can cache files and so on. Problem is, that there are many files to be cached and I´m trying to return an array of paths to the service worker, so that the files can be added to cache without adding them manually.



      Here´s what I have so far:



      Controller:



          public ActionResult GetFilesToCache()
      {
      string filePaths = Directory.GetFiles(Server.MapPath(@"~Content"), "*", SearchOption.AllDirectories);
      string cuttedFiles = new string[filePaths.Length];
      int i = 0;

      foreach (var path in filePaths)
      {
      cuttedFiles[i] = path.Substring(path.IndexOf("Content"));
      i++;
      }

      return Json(new { filesToCache = cuttedFiles }, JsonRequestBehavior.AllowGet);
      }


      This gives me a string array with entries like "Contentimage1.png" etc.



      Service worker:



          self.addEventListener('install', function(e) {
      console.log('[ServiceWorker] Install');
      e.waitUntil(
      caches.open(cacheName).then(function (cache) {
      console.log('[ServiceWorker] Caching app shell');

      return fetch('Home/GetFilesToCache').then(function (response) {
      return response;
      }).then(function (files) {
      return cache.addAll(files);
      });
      })
      );
      });


      The error I get is:



      Uncaught (in promise) TypeError: Failed to execute 'addAll' on 'Cache': Iterator getter is not callable.


      Calling the action works just fine, data is received by the service worker, but not added to cache.







      asp.net-mvc service-worker






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 8:44

























      asked Nov 8 at 14:23









      WhoMightThisOneBe

      1479




      1479
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          In the following code:



          return fetch('Home/GetFilesToCache').then(function (response) {
          return response;
          }).then(function (files) {
          return cache.addAll(files);
          });


          the value of the files parameter is going to be a Response object. You want it to be the JSON deserialization of the Response object's body. You can get this by changing return response with return response.json(), leading to:



          return fetch('Home/GetFilesToCache').then(function(response) {
          return response.json();
          }).then(function(files) {
          return cache.addAll(files);
          });





          share|improve this answer





















          • Thank you for your answer, but it didn´t work: Uncaught (in promise) TypeError: Failed to execute 'addAll' on 'Cache': Iterator getter is not callable.
            – WhoMightThisOneBe
            Nov 9 at 5:10












          • Setting a breakpoint at .then(function(files)) revealed that files is an array with 84 elements, which I expected it to be. But I don´t know why that error message comes up.
            – WhoMightThisOneBe
            Nov 9 at 5:19


















          up vote
          1
          down vote













          Got it working with this code:



              self.addEventListener('install', function(e) {
          console.log('[ServiceWorker] Install');
          e.waitUntil(
          caches.open(cacheName).then(function (cache) {
          console.log('[ServiceWorker] Caching app shell');

          return fetch('Home/GetFilesToCache').then(function (response) {
          return response.json();
          }).then(function (files) {
          var array = files.filesToCache;
          return cache.addAll(array);
          });
          })
          );
          });


          Notice:
          Chrome only lists a part of files stored in cache, so you just have to click that little arrow to show the next page:



          enter image description here






          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%2f53209698%2fusing-a-service-worker-to-get-list-of-files-to-cache-from-server%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            In the following code:



            return fetch('Home/GetFilesToCache').then(function (response) {
            return response;
            }).then(function (files) {
            return cache.addAll(files);
            });


            the value of the files parameter is going to be a Response object. You want it to be the JSON deserialization of the Response object's body. You can get this by changing return response with return response.json(), leading to:



            return fetch('Home/GetFilesToCache').then(function(response) {
            return response.json();
            }).then(function(files) {
            return cache.addAll(files);
            });





            share|improve this answer





















            • Thank you for your answer, but it didn´t work: Uncaught (in promise) TypeError: Failed to execute 'addAll' on 'Cache': Iterator getter is not callable.
              – WhoMightThisOneBe
              Nov 9 at 5:10












            • Setting a breakpoint at .then(function(files)) revealed that files is an array with 84 elements, which I expected it to be. But I don´t know why that error message comes up.
              – WhoMightThisOneBe
              Nov 9 at 5:19















            up vote
            1
            down vote













            In the following code:



            return fetch('Home/GetFilesToCache').then(function (response) {
            return response;
            }).then(function (files) {
            return cache.addAll(files);
            });


            the value of the files parameter is going to be a Response object. You want it to be the JSON deserialization of the Response object's body. You can get this by changing return response with return response.json(), leading to:



            return fetch('Home/GetFilesToCache').then(function(response) {
            return response.json();
            }).then(function(files) {
            return cache.addAll(files);
            });





            share|improve this answer





















            • Thank you for your answer, but it didn´t work: Uncaught (in promise) TypeError: Failed to execute 'addAll' on 'Cache': Iterator getter is not callable.
              – WhoMightThisOneBe
              Nov 9 at 5:10












            • Setting a breakpoint at .then(function(files)) revealed that files is an array with 84 elements, which I expected it to be. But I don´t know why that error message comes up.
              – WhoMightThisOneBe
              Nov 9 at 5:19













            up vote
            1
            down vote










            up vote
            1
            down vote









            In the following code:



            return fetch('Home/GetFilesToCache').then(function (response) {
            return response;
            }).then(function (files) {
            return cache.addAll(files);
            });


            the value of the files parameter is going to be a Response object. You want it to be the JSON deserialization of the Response object's body. You can get this by changing return response with return response.json(), leading to:



            return fetch('Home/GetFilesToCache').then(function(response) {
            return response.json();
            }).then(function(files) {
            return cache.addAll(files);
            });





            share|improve this answer












            In the following code:



            return fetch('Home/GetFilesToCache').then(function (response) {
            return response;
            }).then(function (files) {
            return cache.addAll(files);
            });


            the value of the files parameter is going to be a Response object. You want it to be the JSON deserialization of the Response object's body. You can get this by changing return response with return response.json(), leading to:



            return fetch('Home/GetFilesToCache').then(function(response) {
            return response.json();
            }).then(function(files) {
            return cache.addAll(files);
            });






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 8 at 17:19









            Jeff Posnick

            28.4k46091




            28.4k46091












            • Thank you for your answer, but it didn´t work: Uncaught (in promise) TypeError: Failed to execute 'addAll' on 'Cache': Iterator getter is not callable.
              – WhoMightThisOneBe
              Nov 9 at 5:10












            • Setting a breakpoint at .then(function(files)) revealed that files is an array with 84 elements, which I expected it to be. But I don´t know why that error message comes up.
              – WhoMightThisOneBe
              Nov 9 at 5:19


















            • Thank you for your answer, but it didn´t work: Uncaught (in promise) TypeError: Failed to execute 'addAll' on 'Cache': Iterator getter is not callable.
              – WhoMightThisOneBe
              Nov 9 at 5:10












            • Setting a breakpoint at .then(function(files)) revealed that files is an array with 84 elements, which I expected it to be. But I don´t know why that error message comes up.
              – WhoMightThisOneBe
              Nov 9 at 5:19
















            Thank you for your answer, but it didn´t work: Uncaught (in promise) TypeError: Failed to execute 'addAll' on 'Cache': Iterator getter is not callable.
            – WhoMightThisOneBe
            Nov 9 at 5:10






            Thank you for your answer, but it didn´t work: Uncaught (in promise) TypeError: Failed to execute 'addAll' on 'Cache': Iterator getter is not callable.
            – WhoMightThisOneBe
            Nov 9 at 5:10














            Setting a breakpoint at .then(function(files)) revealed that files is an array with 84 elements, which I expected it to be. But I don´t know why that error message comes up.
            – WhoMightThisOneBe
            Nov 9 at 5:19




            Setting a breakpoint at .then(function(files)) revealed that files is an array with 84 elements, which I expected it to be. But I don´t know why that error message comes up.
            – WhoMightThisOneBe
            Nov 9 at 5:19












            up vote
            1
            down vote













            Got it working with this code:



                self.addEventListener('install', function(e) {
            console.log('[ServiceWorker] Install');
            e.waitUntil(
            caches.open(cacheName).then(function (cache) {
            console.log('[ServiceWorker] Caching app shell');

            return fetch('Home/GetFilesToCache').then(function (response) {
            return response.json();
            }).then(function (files) {
            var array = files.filesToCache;
            return cache.addAll(array);
            });
            })
            );
            });


            Notice:
            Chrome only lists a part of files stored in cache, so you just have to click that little arrow to show the next page:



            enter image description here






            share|improve this answer

























              up vote
              1
              down vote













              Got it working with this code:



                  self.addEventListener('install', function(e) {
              console.log('[ServiceWorker] Install');
              e.waitUntil(
              caches.open(cacheName).then(function (cache) {
              console.log('[ServiceWorker] Caching app shell');

              return fetch('Home/GetFilesToCache').then(function (response) {
              return response.json();
              }).then(function (files) {
              var array = files.filesToCache;
              return cache.addAll(array);
              });
              })
              );
              });


              Notice:
              Chrome only lists a part of files stored in cache, so you just have to click that little arrow to show the next page:



              enter image description here






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                Got it working with this code:



                    self.addEventListener('install', function(e) {
                console.log('[ServiceWorker] Install');
                e.waitUntil(
                caches.open(cacheName).then(function (cache) {
                console.log('[ServiceWorker] Caching app shell');

                return fetch('Home/GetFilesToCache').then(function (response) {
                return response.json();
                }).then(function (files) {
                var array = files.filesToCache;
                return cache.addAll(array);
                });
                })
                );
                });


                Notice:
                Chrome only lists a part of files stored in cache, so you just have to click that little arrow to show the next page:



                enter image description here






                share|improve this answer












                Got it working with this code:



                    self.addEventListener('install', function(e) {
                console.log('[ServiceWorker] Install');
                e.waitUntil(
                caches.open(cacheName).then(function (cache) {
                console.log('[ServiceWorker] Caching app shell');

                return fetch('Home/GetFilesToCache').then(function (response) {
                return response.json();
                }).then(function (files) {
                var array = files.filesToCache;
                return cache.addAll(array);
                });
                })
                );
                });


                Notice:
                Chrome only lists a part of files stored in cache, so you just have to click that little arrow to show the next page:



                enter image description here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 8:43









                WhoMightThisOneBe

                1479




                1479






























                    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%2f53209698%2fusing-a-service-worker-to-get-list-of-files-to-cache-from-server%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







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python