问题
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);
});
回答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").
来源:https://stackoverflow.com/questions/48283710/sw-precache-not-updating-and-taking-only-cached-data