How to clear cache of service worker?

前端 未结 4 888
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 05:43

So, I have an HTML page with service worker, the service worker cache the index.html and my JS files.

The problem is when I change the JS, the change doesn\'t show u

4条回答
  •  独厮守ぢ
    2020-11-28 06:06

    Typically you update the CACHE_NAME in your service workers JS file so your worker installs again:

    self.addEventListener('install', evt => {
      evt.waitUntil(
        caches.open(CACHE_NAME).then(cache => cache.addAll(inputs))
      )
    })
    

    Alternatively, to clear the cache for a PWA find the cache name:

    self.caches.keys().then(keys => { keys.forEach(key => console.log(key)) })
    

    then run the following to delete it:

    self.caches.delete('my-site-cache')
    

    Then refresh the page.

    If you see any worker-related errors in the console after refreshing, you may also need to unregister the registered workers:

    navigator.serviceWorker.getRegistrations()
      .then(registrations => {
        registrations.forEach(registration => {
          registration.unregister()
        }) 
      })
    

提交回复
热议问题