如何在Service_worker.js所在的目录上注册服务工作者的作用域 [英] How to register a service worker's scope one directory above where the service_worker.js is located

查看:317
本文介绍了如何在Service_worker.js所在的目录上注册服务工作者的作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目录如下。

public_html/
sw/

sw /是我想放置所有服务工作者的地方,但是那些服务工作者的范围是的public_html /。

The "sw/" is where I want to put all service workers, but then have those service workers with a scope to all the files in "public_html/".

JS

<script>
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('sw/notifications.js', { scope: '../sw/' }).then(function(reg) {
    // registration worked
    console.log('Registration succeeded. Scope is ' + reg.scope);
  }).catch(function(error) {
    // registration failed
    console.log('Registration failed with ' + error);
  });
};
</script>

我如何允许这种范围?

推荐答案

服务工作者的最大范围是它位于。这意味着您无法在范围:'/ public_html /'中注册位于 / sw / 的一名服务工作人员,除非您包括一个特殊标题 Service-Worker-Allowed 设置为服务工作者的新最大范围。

The max scope for a service worker is where it is located. This means you can not register one service worker located at /sw/ in scope: '/public_html/' unless you include a special header Service-Worker-Allowed set to the new max scope for your service worker.

汇总,如果您可以在为服务工作者提供服务时添加此标头,请将其设置如下:

Summarizing, if you can add this header when serving the service worker, set it as follows:

Service-Worker-Allowed: /public_html/

如果没有,您必须将sw放在范围上方的某个位置。

If not, you must place the sw at some location above the scope.

这篇关于如何在Service_worker.js所在的目录上注册服务工作者的作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆