如何在service_worker.js所在目录的上方注册一个service worker的scope [英] How to register a service worker's scope one directory above where the service_worker.js is located
问题描述
我的目录如下.
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>
我如何允许这种范围?
推荐答案
最大范围Service Worker 位于它所在的位置.这意味着你不能在 scope: '/public_html/'
中注册一个位于 /sw/
的服务工作者,除非你包含一个特殊的标题 Service-Worker-Allowed
设置为 Service Worker 的新最大范围.
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.
总结一下,如果可以在服务service worker的时候加上这个header,设置如下:
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所在目录的上方注册一个service worker的scope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!