我正在尝试Service Worker在测试页面中实现一个.我的最终目标是离线操作的应用程序.文件夹结构如下
/myApp
...
/static
/mod
/practice
service-worker.js
worker-directives.js
foopage.js
/templates
/practice
foopage.html
Run Code Online (Sandbox Code Playgroud)
我正在注册服务工作者,如下所示(内service-worker.js):
navigator.serviceWorker.register('../static/mod/practice/service-worker.js').then(function(reg) {
console.log('Registration succeeded. Scope is ' + reg.scope);
...
}
Run Code Online (Sandbox Code Playgroud)
在控制台中,我看到了
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) {
...
}
Run Code Online (Sandbox Code Playgroud)
我收到了错误
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 …Run Code Online (Sandbox Code Playgroud)