服务工作者似乎没有更新

use*_*848 5 javascript service-worker

我正在关注服务工作者的以下html5 rock教程(http://www.html5rocks.com/en/tutorials/service-worker/introduction/#disqus_thread),我遇到了我的服务工作者没有的问题.我想要更新.

我有一个名为"app.js"的文件,其中包含以下代码:

navigator.serviceWorker.register('worker.js').then(function(reg) {
  console.log('???', reg);
}, function(err) {
  console.log('?_?', err);
});
Run Code Online (Sandbox Code Playgroud)

我还有一个名为"worker.js"的文件,其中包含以下代码:

console.log("SW startup");
var myCache = 'myapp-static-v3';
var urlsToCache = [ 
  '/',
  '/assets/style.css',
  'app.js'
];

self.addEventListener('install', function(event) {
  console.log("SW installed");
  // pre cache a load of stuff:
  event.waitUntil(
    caches.open(myCache).then(function(cache) {
      console.log("opened cache");
      return cache.addAll(urlsToCache);
    })
  )
});

self.addEventListener('activate', function(event) {

  var cacheWhitelist = [myCache];

   event.waitUntil(
    caches.keys().then(function(cacheNames) {
      return Promise.all(
        cacheNames.map(function(cacheName) {
          if (cacheWhitelist.indexOf(cacheName) === -1) {
            return caches.delete(cacheName);
          }
        })
      );
    })
  );
});

self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request)
      .then(function(response) {
        // Cache hit - return response
        if (response) {
          return response;
        }

        var fetchRequest = event.request.clone();

        return fetch(fetchRequest).then(
          function(response) {
            // Check if we received a valid response
            if(!response || response.status !== 200 || response.type  !== 'basic') {
              return response;
        }

        var responseToCache = response.clone();

        caches.open(CACHE_NAME)
          .then(function(cache) {
            cache.put(event.request, responseToCache);
          });

        return response;
      }
    );
  })
);
});
Run Code Online (Sandbox Code Playgroud)

我尝试更新实际的myCache变量名,使用不同的文件切换style.css并实际更改内容.

任何人都知道我做错了什么?

oni*_*oss 0

在此输入图像描述

根据文档,必须更改的是 Service Worker 的实际文件。我在 Service Worker 更新中所做的就是更改版本号。在你的情况下,它可能是var myCache = 'myapp-static-v3';

您可以执行诸如语义版本控制之类的操作。
var myCache = 'myapp-static-v3.0.0'然后升级为var myCache = 'myapp-static-v3.0.1';