简体   繁体   English

更改服务工作者的姓名?

[英]Changing the name of a service worker?

I'm trying to figure out what happens if I have a service worker registered on a live site called sw.js and then I rename the service worker to service-worker.js . 我想弄清楚如果我在一个名为sw.js的实际站点上注册的服务工作者,然后我将服务工作者重命名为service-worker.js Now the old one isn't found but it is still showing the old cached version. 现在找不到旧的但它仍然显示旧的缓存版本。

How long does it take for it to register the new renamed service worker or how does this work at all? 注册新的重命名服务工作者需要多长时间,或者这根本有用吗?

Edit 编辑

This is how I have register the service worker in a react application: 这就是我在反应应用程序中注册服务工作者的方法:

componentDidMount() {
  if ("serviceWorker" in navigator) {
    navigator.serviceWorker
      .register("/service-worker.js")
      .then(registration => {
        console.log("service worker registration successful: ", registration);
      })
      .catch(err => {
        console.warn("service worker registration failed", err.message);
      });
  }
}

The newly created service worker (renamed) cannot take over the old one because the old one is still active and controlling the client. 新创建的服务工作者(已重命名)无法接管旧服务工作者,因为旧服务工作者仍处于活动状态并控制客户端。 the new service worker(renamed one) will wait until the existing worker is controlling zero clients. 新服务工作者(重命名为)将等到现有工作人员控制零客户端。

Now imagine a service worker sw.js installed and active (controlling the client), Chrome will visualize the process for you like this 现在想象一下服务工作者sw.js安装并激活(控制客户端),Chrome会像这样为您显示流程

1. The service worker is registered and active 1.服务人员已注册并处于活动状态

在此输入图像描述


2. Now let's rename the service worker file to sw2.js 2.现在让我们将服务工作者文件重命名为sw2.js

在此输入图像描述

You can see that chrome is telling you that something has changed about the service worker. 您可以看到chrome告诉您有关服务工作者的更改。 but the current one will keep controlling the client until you force the new one to take control by clicking on the skipWaitting button or by flushing your cache. 但是当前的一个将继续控制客户端,直到您通过单击skipWaitting按钮或刷新缓存强制新的客户端来控制。 clicking on the button will cause the sw2.js to take controll over the sw1.js 单击该按钮将使sw2.js控制sw1.js

Now if you need to do this programmatically, you can do it in the install event inside your service worker by calling self.skipWaiting() . 现在,如果需要以编程方式执行此操作,可以通过调用self.skipWaiting()在服务工作者内部的install事件中执行此操作。

self.addEventListener('install', (e) => {

  let cache = caches.open(cacheName).then((c) => {
    c.addAll([
      // my files
    ]);
  });

  self.skipWaiting();
  e.waitUntil(cache);
});

The following animated image from Jake Archibald's article The Service Worker Lifecycle can make the idea more clear. 来自Jake Archibald的文章The Service Worker Lifecycle的以下动画图像可以使这个想法更加清晰。

You have to update the instance creation code to reflect this change where your shared worker is being initialized and used, for example your current code would look like 您必须更新实例创建代码以反映您的共享工作程序初始化和使用的位置,例如您当前的代码看起来像

var worker = new SharedWorker("ws.js");

That will need to be updated to 这将需要更新到

var worker = new SharedWorker("service-worker.js");

I was able to solve it by setting up a server which listens to both /service-worker.js and /sw.js get requests. 我能够通过设置一个服务器来解决它,该服务器监听/service-worker.js/sw.js获取请求。

Since the service worker was renamed from sw.js to service-worker.js it was not finding the old service worker at http://example.com/sw.js so what I did was the following: 由于服务工作者已从sw.js重命名为service-worker.js ,因此未在http://example.com/sw.js找到旧服务工作者,因此我所做的如下:

createServer((req, res) => {
  const parsedUrl = parse(req.url, true);
  const { pathname } = parsedUrl;
  // new service worker
  if (pathname === "/service-worker.js") {
    const filePath = join(__dirname, "..", pathname);
    app.serveStatic(req, res, filePath);
  // added new endpoint to fetch the new service worker but with the 
  // old path
  } else if (pathname === "/sw.js") {
    const filePath = join(__dirname, "..", "/service-worker.js");
    app.serveStatic(req, res, filePath);
  } else {
    handle(req, res, parsedUrl);
  }
}).listen(port, err => {
  if (err) throw err;
  console.log(`> Ready on http://localhost:${port}`);
});

As you can see, I added a second path to serve the same service worker but with the /sw.js endpoint, one for the old sw.js and the other one for the newer service-worker.js . 正如您所看到的,我添加了第二条路径来为同一个服务工作者提供服务,但是使用/sw.js端点,一个用于旧sw.js ,另一个用于较新的service-worker.js

Now when old visitors that have the old active sw.js will download the newer one and upon revisit, they will automatically fetch the newer renamed service-worker.js service worker. 现在,当具有旧的活动sw.js的旧sw.js将下载较新的访问者并重新访问时,他们将自动获取较新的重命名的service-worker.js服务工作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM