我正在尝试Service Worker
在测试页面中实现 a 。我的最终目标是一个离线运行的应用程序。文件夹结构如下
/myApp
...
/static
/mod
/practice
service-worker.js
worker-directives.js
foopage.js
/templates
/practice
foopage.html
我正在注册一个服务工作者,如下所示(在 内service-worker.js
):
navigator.serviceWorker.register('../static/mod/practice/service-worker.js').then(function(reg) {
console.log('Registration succeeded. Scope is ' + reg.scope);
...
}
在控制台中我看到
Registration succeeded. Scope is https://example.com/static/mod/practice/
如果我的页面位于https://example.com/practice/foopage
,我是否需要确保我的service worker
范围是https://example.com/practice/foopage
?
如果我尝试在register
函数调用中定义范围,例如
navigator.serviceWorker.register('../static/mod/practice/service-worker.js', { scope: '/practice/foopage/' }).then(function(reg) {
...
}
我收到错误
Registration failed with SecurityError: Failed to register a ServiceWorker: The path of the provided scope ('/practice/foopage/') is not under the max scope allowed ('/static/mod/practice/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.
问题是:范围究竟指的是什么?它是service worker
最终控制的 URL 集合吗?我需要搬到service-workers.js
别的地方吗?如果有,在哪里?