在同一个服务工作者中处理 service-worker.js 有什么影响吗?

Is there any effect of handling service-worker.js in the same service worker?

假设我的服务人员是 sw.js。如果我使用相同的服务工作者 (sw.js) 预缓存或缓存此文件,会有什么影响吗?

例如,使用sw-toolbox

toolbox.precache(['sw.js']);
toolbox.router.get('/sw.js', toolbox.cacheOnly);

并且

navigator.serviceWorker.register('sw.js');

现在,这个 service-worker 是否应该更新,或者浏览器是否允许一个特殊的获取来检查 URL 是否与活动的 ServiceWorkerURL 完全匹配并遵守 24 小时制强制更新service worker并打网络?

来自https://www.w3.org/TR/service-workers/#service-worker-registration-update

update() pings the server for an updated version of this script without consulting caches. This is conceptually the same operation that UA does maximum once per every 24 hours.

此外,来自 https://www.w3.org/TR/service-workers/#update-algorithm,步骤 4.8:

Let response be the result of running fetch using r, forcing a network fetch if cached entry is greater than 1 day old.

没有.

更新请求根本不通过 service-worker。

根据 ServiceWorker 规范 -

更新算法中的点 5.2 - https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#update-algorithm

Set request’s skip service worker flag and request’s redirect mode to "error".

并且 fetch 有一个 https://fetch.spec.whatwg.org/#skip-service-worker-flag

  1. 设置只为一个WebSocket connection (Point 2)
  2. NOT set or unset for all requests out out from the Document - HTTP Fetch (Point 3)
  3. 此标志公开,您不能在请求中使用它。

对于 service worker 更新,此标志是 set 并且它不经过 service worker 生命周期事件 - 获取。交叉检查,

toolbox.router.get('/sw.js', function(...args) {
  // never called
  console.log(...args);
  return toolbox.cacheOnly(...args);
});

使用此 SW 代码并尝试刷新页面,您永远不会使用此功能来更新 service worker。仅当您

时才会调用此函数
fetch('/sw.js')

来自文档。