1

Iam trying to implement networkfirst strategy using sw-precache.
Now iam able to cache the data and able to serve from offline. If i change the data (i.e: changed the header from 'hello' to 'Welcome') in page not get reflecting it always taking the data from the cache its getting update only if i unregistered the service worker or clear the site data then only i can get my data

Here is my sw-precache gulp task :

gulp.task('generate-service-worker', function(callback) {
  var path = require('path');
  var swPrecache = require('sw-precache');
  var rootDir = '.';

  swPrecache.write(path.join(rootDir, 'sw.js'), {
    staticFileGlobs: [
      rootDir + '/css/**.css',
      rootDir + '/js/**/*.{js,css}',
      rootDir + '/images/**/*.{png,jpg,jpeg}',
      rootDir + '/*.{html,js}',
    ],
    runtimeCaching: [
      {
        urlPattern: 'http://localhost:8080',
        handler: 'networkFirst'
      }],
    stripPrefix: rootDir
  }, callback); 
});
Harish Karthick
  • 710
  • 2
  • 12
  • 26

1 Answers1

1

Two things to check:

  • Ensure that you're not caching your sw.js file, as this could delay updates for up to 24 hours in Chrome. (Details.)
  • You're checking for updating content on the subsequent visit to the site following your update? Because of the cache-first strategy, the initial visit to the site following the update won't show the new content (because the cache has been updated "in the background").
Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
  • I modified the gulp file to cache sw.js,but I couldn't able to cache sw.js , even though i use network first i am having the same problem – Harish Karthick Jan 18 '18 at 05:19
  • I meant that you should **not** cache `sw.js` via `Cache-Control` headers. See the SO answer I linked to for more details. – Jeff Posnick Jan 18 '18 at 15:42
  • Oh sorry @Jeff for misunderstanding now i checked the cache memory and i didn't cache the "sw.js" i getting my page update if I checked the update on reloading in dom or clear the cache else i could not get updated content – Harish Karthick Jan 18 '18 at 17:39
  • Could you share the URL for your deployed site? – Jeff Posnick Jan 18 '18 at 18:14
  • I am checking it in local machine only – Harish Karthick Jan 18 '18 at 18:19
  • could you please help to get out this problem – Harish Karthick Jan 19 '18 at 18:28
  • I don't have any specific suggestions in general beyond what I've already made. If you could deploy this somewhere public, that would help debug, but without access to your web app there's not much more that I could contribute. – Jeff Posnick Jan 19 '18 at 19:23
  • I took a look at https://pwa.vamsoft.in/sw.js and you're not in face using `sw-precache`, as you mentioned in your original question. Have you changed your site since originally asking? – Jeff Posnick Jan 30 '18 at 00:47
  • Yes If I changed the HTML contents in the home page then the old one is getting shown unless until I unregister the service worker and reload the page – Harish Karthick Jan 30 '18 at 00:51
  • My point is that you asked a question about `sw-precache`, but you're not actually using it. – Jeff Posnick Jan 30 '18 at 01:08
  • Apologies for that, Kindly check it now. – Harish Karthick Jan 30 '18 at 02:12
  • 1
    I took a look at your site—given that you're preaching your `/` URL via `sw-precache`, it's going to be served in a cache-first strategy (`sw-precache` does everything cache-first). That means that you won't see updated until the *next* visit to the site following a deployment. This is the expected behavior, and if you want different behavior, you need to exclude your HTML from being precached. – Jeff Posnick Jan 30 '18 at 18:07
  • And also tell which is the best way to cache HTML pages – Harish Karthick Feb 01 '18 at 01:19
  • @jeffPosnik kindly share the above prerequisite a have asked – Harish Karthick Feb 03 '18 at 17:25
  • 1
    The comments thread of a previously answered Stack Overflow question is not the best place to ask for follow-ups. Could you please ask a new question with details of what you're looking for? – Jeff Posnick Feb 05 '18 at 17:42
  • @JeffPosnick BTW if you exclude index.html from being cached, then offline mode will not work. Right? What is the solution for this? – the_haystacker Feb 11 '18 at 05:12
  • @JeffPosnick kindly help my question : https://stackoverflow.com/questions/51586398/pwa-add-to-home-screen-not-showing-popup?noredirect=1#comment90156355_51586398 – Harish Karthick Jul 30 '18 at 14:04