Second update service worker doesn't work

▼魔方 西西 提交于 2021-01-29 17:19:49

问题


I hava a pwa with this sw.js:

const info = "versione: 1.0.0 data: 07-01-2020 12:46";
const CACHE = "pwa-offline";
const pages = [
  // some files
];

self.addEventListener("install", function (event) {
  //console.log("Install Event processing");
  self.skipWaiting();

  event.waitUntil(
    caches.open(CACHE).then(function (cache) {
      //console.log("Cached offline page during install");
      return cache.addAll(pages);
    })
  );
});

self.addEventListener("activate", (event) => {
  
});

self.addEventListener("fetch", function (event) {
  if (event.request.method !== "GET") return;
  event.respondWith(
    fetch(event.request)
    .then(function (response) {
      return fromCache(event.request);
    })
    
    .catch(function (error) {
      //console.log("Network request Failed. Serving content from cache: " + error);
      return fromCache(event.request);
    })
  );
});

async function fromCache(request) {
  // Check to see if you have it in the cache
  // Return response
  // If not in the cache, then return error page
  const cache = await caches.open(CACHE);
  const matching = await cache.match(request);
  if (!matching || matching.status === 404) {
    return Promise.reject("no-match");
  }
  return matching;
}

async function updateCache(request, response) {
  const cache = await caches.open(CACHE);
  return cache.put(request, response);
}

and index.html with this code inside:

 <script>
    if ("serviceWorker" in navigator) {
      navigator.serviceWorker
        .register("./sw.js")//, {updateViaCache: 'none'})
        .then(reg => {
          //console.log("Registration successful", reg);
        })
        .catch(e =>
          console.error("Error during service worker registration:", e)
        );
    } else {
      console.warn("Service Worker is not supported");
    }
  </script>

I upload the pwa in a firebase site. When I change the sw.js (e.g. changing the date of versionDate, in this site https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle I read: "Your service worker is considered updated if it's byte-different to the one the browser already has. (We're extending this to include imported scripts/modules too.)") and I upload, I see the new service worker is changed. But when I make a second change in sw.js and upload, the sw.js is not changed, so I can do only an update on sw.js and so to the whole site (because the sw.js caches all files of the site during the install process). How can I update my site any time I want?

UPDATE: I watched that the problem is in android phone in desktop is ok.

UPDATE: Now also it works on android but the update is not immadiate. I see the update after some hours.


回答1:


Your service worker caches all static files on your website. Consequently, you will need to edit your sw.js file whenever you are updating your website. One common way to do this is to attach a version number to your static cache name, for example pwa-offline-v1, then bump up the version number in the sw.js file whenever you are pushing an update to the site. This creates a new cache and stores the updated static files to it. You can then add an activate event listener on the service worker to delete the former cache using a function like this.

const info = "versione: 1.0.0 data: 07-01-2020 12:46";
const CACHE = "pwa-offline-v1";
const pages = [
  // some files
];

self.addEventListener("install", function (event) {
  //console.log("Install Event processing");
  self.skipWaiting();

  event.waitUntil(
    caches.open(CACHE).then(function (cache) {
      //console.log("Cached offline page during install");
      return cache.addAll(pages);
    })
  );
});

self.addEventListener("activate", (event) => {
  event.waitUntil(
    Promise.all(
      caches.keys().then((cacheNames) => {
        cacheNames
          .filter(cacheName => cacheName.startsWith('pwa-offline-') && cacheName !== CACHE)
          .map(cacheName => caches.delete(cacheName))
      })
    )
  );
});


来源:https://stackoverflow.com/questions/65630252/second-update-service-worker-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!