我正在尝试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) 所以我正在构建一个Django渐进式Web应用程序,使用服务工作者提供离线支持.
根据谷歌的文档,sw.js文件应该是应用程序URL的根目录:
您需要执行此操作,因为服务工作者的范围(ServiceWorker将加载的URL集)由其所在的目录定义.
目前,我正在从http://example.com/static/文件夹中提供所有静态资源.但我需要在以下网址上提供此特定文件:http://example.com/sw.js.
知道我怎么能做到这一点?我可以制定一个特定的nginx规则来执行此重定向,但我不知道这是否是最干净的方法.也许这个设置应该存在于urls.py中?
注意:我已经看到这个问题建议使用static()方法django.conf.urls.static.但是django的文档说静态方法仅供开发使用,对我不利.
注意(2):我想我可以更改STATIC_URL设置,但我对从/ static目录提供的文件感到满意.我只希望这个文件位于url的根目录下.