服务工作者和CSP配置更改

时间:2017-05-31 10:17:48

标签: service-worker content-security-policy

在我们的项目中,我们已配置CSP并传入Response Headers。我们还有简单的服务工作者,它检查是否可以导航到另一个页面,如果没有重定向到缓存的离线html页面。 这是服务工作者的一部分代码,用于获取事件

self.addEventListener('fetch', function (event) {
  event.respondWith(
    // Try to find requested resource in the cache
    caches
      .match(event.request).then(function (response) {
        // Fallback to network if it's not in cache
        return response || fetch(event.request);
      })
      .catch(getFallbackResponse(event))
    );
});

但是当CSP配置发生变化并且在CSP配置发生这种变化之前安装了Service Worker时,我们得到了     Refused to load the script '[url]' because it violates the following Content Security Policy directive: ...错误。一旦我们更新或取消注册Service Worker,就会应用新的CSP配置。

这是预期的行为吗?

1 个答案:

答案 0 :(得分:0)

如果您查看MDN对skip​Waiting()的描述,似乎需要服务人员立即进行更新:

  

强制等待服务的工作人员成为活动服务人员。

     

将此方法与Clients.claim()结合使用,以确保对基础服务工作者的更新立即对当前客户端和所有其他活动客户端都生效。

Clients.claim()是为了确保还可能更新了其他打开的选项卡中的服务程序。