Why is my service worker only caching visited pages?










0















Im currently developing a simple PWA with an index file that contains an inputfield where you can enter your medication barcode and hit submit. A PHP-file will then be displayed showing some information about the drugs recieved from a MySQL database.



Im now trying to cache the app shell in order to load the necessairy resources (html, css, scripts etc.) once the app is working offline. I kinda succeeded in this, but it only seems to work when the page that you're trying to cache is already visited once when the app was online. And this only is the case when I register that same service-worker on the result page, not only on the index page.



Apparantly, this kind of caching in which only the visited pages are cached is called 'partial' caching by my professor. I don't want this partial caching, I want a complete caching in which the service-worker is registered once on the index file and then caches all the necessairy files into to cache so that I can use it when offline.



This is how my service-worker looks right:



var cacheName = 'pillexplainerPWA';
var filesToCache = [
'http://localhost:80/MedicationProject/',
'http://localhost:80/MedicationProject/index.html',
'http://localhost:80/MedicationProject/result.php',
'http://localhost:80/MedicationProject/result2.html',
'http://localhost:80/MedicationProject/scripts/app.js',
'http://localhost:80/MedicationProject/styles/main.css',
'http://localhost:80/MedicationProject/styles/result.css',
'http://localhost:80/MedicationProject/images/back-button.jpg',
'http://localhost:80/MedicationProject/images/barcode.jpg'
];

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

self.addEventListener('activate', function(e)
console.log('[ServiceWorker] Activate');
e.waitUntil(
caches.keys().then(function(keyList)
return Promise.all(keyList.map(function(key)
if (key !== cacheName)
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);

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

self.addEventListener('fetch', function(e)
console.log('[ServiceWorker] Fetch', e.request.url);
e.respondWith(
caches.match(e.request).then(function(response) )
);
);


Can someone explain to me what I'm doing wrong and what needs to be adjusted to this code so that it won't cache only what is visited but everything the moment that index.html is loaded? The strange thing is, when I look into the cache when index.html is fired..it does show me all my files being there. But when I go offline and then hit that submit button to go to the result page, it doesn't load the cache.










share|improve this question


























    0















    Im currently developing a simple PWA with an index file that contains an inputfield where you can enter your medication barcode and hit submit. A PHP-file will then be displayed showing some information about the drugs recieved from a MySQL database.



    Im now trying to cache the app shell in order to load the necessairy resources (html, css, scripts etc.) once the app is working offline. I kinda succeeded in this, but it only seems to work when the page that you're trying to cache is already visited once when the app was online. And this only is the case when I register that same service-worker on the result page, not only on the index page.



    Apparantly, this kind of caching in which only the visited pages are cached is called 'partial' caching by my professor. I don't want this partial caching, I want a complete caching in which the service-worker is registered once on the index file and then caches all the necessairy files into to cache so that I can use it when offline.



    This is how my service-worker looks right:



    var cacheName = 'pillexplainerPWA';
    var filesToCache = [
    'http://localhost:80/MedicationProject/',
    'http://localhost:80/MedicationProject/index.html',
    'http://localhost:80/MedicationProject/result.php',
    'http://localhost:80/MedicationProject/result2.html',
    'http://localhost:80/MedicationProject/scripts/app.js',
    'http://localhost:80/MedicationProject/styles/main.css',
    'http://localhost:80/MedicationProject/styles/result.css',
    'http://localhost:80/MedicationProject/images/back-button.jpg',
    'http://localhost:80/MedicationProject/images/barcode.jpg'
    ];

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

    self.addEventListener('activate', function(e)
    console.log('[ServiceWorker] Activate');
    e.waitUntil(
    caches.keys().then(function(keyList)
    return Promise.all(keyList.map(function(key)
    if (key !== cacheName)
    console.log('[ServiceWorker] Removing old cache', key);
    return caches.delete(key);

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

    self.addEventListener('fetch', function(e)
    console.log('[ServiceWorker] Fetch', e.request.url);
    e.respondWith(
    caches.match(e.request).then(function(response) )
    );
    );


    Can someone explain to me what I'm doing wrong and what needs to be adjusted to this code so that it won't cache only what is visited but everything the moment that index.html is loaded? The strange thing is, when I look into the cache when index.html is fired..it does show me all my files being there. But when I go offline and then hit that submit button to go to the result page, it doesn't load the cache.










    share|improve this question
























      0












      0








      0








      Im currently developing a simple PWA with an index file that contains an inputfield where you can enter your medication barcode and hit submit. A PHP-file will then be displayed showing some information about the drugs recieved from a MySQL database.



      Im now trying to cache the app shell in order to load the necessairy resources (html, css, scripts etc.) once the app is working offline. I kinda succeeded in this, but it only seems to work when the page that you're trying to cache is already visited once when the app was online. And this only is the case when I register that same service-worker on the result page, not only on the index page.



      Apparantly, this kind of caching in which only the visited pages are cached is called 'partial' caching by my professor. I don't want this partial caching, I want a complete caching in which the service-worker is registered once on the index file and then caches all the necessairy files into to cache so that I can use it when offline.



      This is how my service-worker looks right:



      var cacheName = 'pillexplainerPWA';
      var filesToCache = [
      'http://localhost:80/MedicationProject/',
      'http://localhost:80/MedicationProject/index.html',
      'http://localhost:80/MedicationProject/result.php',
      'http://localhost:80/MedicationProject/result2.html',
      'http://localhost:80/MedicationProject/scripts/app.js',
      'http://localhost:80/MedicationProject/styles/main.css',
      'http://localhost:80/MedicationProject/styles/result.css',
      'http://localhost:80/MedicationProject/images/back-button.jpg',
      'http://localhost:80/MedicationProject/images/barcode.jpg'
      ];

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

      self.addEventListener('activate', function(e)
      console.log('[ServiceWorker] Activate');
      e.waitUntil(
      caches.keys().then(function(keyList)
      return Promise.all(keyList.map(function(key)
      if (key !== cacheName)
      console.log('[ServiceWorker] Removing old cache', key);
      return caches.delete(key);

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

      self.addEventListener('fetch', function(e)
      console.log('[ServiceWorker] Fetch', e.request.url);
      e.respondWith(
      caches.match(e.request).then(function(response) )
      );
      );


      Can someone explain to me what I'm doing wrong and what needs to be adjusted to this code so that it won't cache only what is visited but everything the moment that index.html is loaded? The strange thing is, when I look into the cache when index.html is fired..it does show me all my files being there. But when I go offline and then hit that submit button to go to the result page, it doesn't load the cache.










      share|improve this question














      Im currently developing a simple PWA with an index file that contains an inputfield where you can enter your medication barcode and hit submit. A PHP-file will then be displayed showing some information about the drugs recieved from a MySQL database.



      Im now trying to cache the app shell in order to load the necessairy resources (html, css, scripts etc.) once the app is working offline. I kinda succeeded in this, but it only seems to work when the page that you're trying to cache is already visited once when the app was online. And this only is the case when I register that same service-worker on the result page, not only on the index page.



      Apparantly, this kind of caching in which only the visited pages are cached is called 'partial' caching by my professor. I don't want this partial caching, I want a complete caching in which the service-worker is registered once on the index file and then caches all the necessairy files into to cache so that I can use it when offline.



      This is how my service-worker looks right:



      var cacheName = 'pillexplainerPWA';
      var filesToCache = [
      'http://localhost:80/MedicationProject/',
      'http://localhost:80/MedicationProject/index.html',
      'http://localhost:80/MedicationProject/result.php',
      'http://localhost:80/MedicationProject/result2.html',
      'http://localhost:80/MedicationProject/scripts/app.js',
      'http://localhost:80/MedicationProject/styles/main.css',
      'http://localhost:80/MedicationProject/styles/result.css',
      'http://localhost:80/MedicationProject/images/back-button.jpg',
      'http://localhost:80/MedicationProject/images/barcode.jpg'
      ];

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

      self.addEventListener('activate', function(e)
      console.log('[ServiceWorker] Activate');
      e.waitUntil(
      caches.keys().then(function(keyList)
      return Promise.all(keyList.map(function(key)
      if (key !== cacheName)
      console.log('[ServiceWorker] Removing old cache', key);
      return caches.delete(key);

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

      self.addEventListener('fetch', function(e)
      console.log('[ServiceWorker] Fetch', e.request.url);
      e.respondWith(
      caches.match(e.request).then(function(response) )
      );
      );


      Can someone explain to me what I'm doing wrong and what needs to be adjusted to this code so that it won't cache only what is visited but everything the moment that index.html is loaded? The strange thing is, when I look into the cache when index.html is fired..it does show me all my files being there. But when I go offline and then hit that submit button to go to the result page, it doesn't load the cache.







      javascript html service-worker






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 3:05









      ToufikToufik

      287




      287






















          0






          active

          oldest

          votes











          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',
          autoActivateHeartbeat: false,
          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%2f53292606%2fwhy-is-my-service-worker-only-caching-visited-pages%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292606%2fwhy-is-my-service-worker-only-caching-visited-pages%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