JavaScript Notifications and Service Worker API: 为什么注册后serviceWorker.ready promise?
JavaScript Notifications and Service Worker API: Why is the serviceWorker.ready promise after registration?
我尝试使用 JavaScript 为我的网络应用程序创建一个简单的通知功能。为此,我尝试注册一个服务人员,它将发送通知以支持通知 API 之类的 action
.
这是我的代码:
1 navigator.serviceWorker.register('/scripts/dummy.js');
2
3 function notify(title, data) {
4 console.log('function call');
5 if (Notification.permission == 'granted' && defaultStatus != 'dnd') {
6 console.log('permission and not dnd');
7 navigator.serviceWorker.ready.then(function(registration) {
8 console.log('ready, send notification);
9 registration.showNotification(title, data);
10 });
11 }
12 }
问题出在navigator.serviceWorker.ready
Chrome 开发者控制台显示 this。我需要在 dummy.js
中添加一些东西吗? (目前是空的,但我认为它以前有效)
我解决了我的问题。它不起作用的原因是因为我将 dummy.js
从当前目录移动到子目录中。
这是我在将选项 { scope: '/' }
添加到 serviceWorker.register
函数后从 Chrome 得到的错误:
The path of the provided scope ('/') is not under the max scope allowed ('/scripts/user/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.
(index):1 Uncaught (in promise) DOMException: Failed to register a ServiceWorker for scope ('https://mydomain/') with script ('https://mydomain/scripts/user/user_worker.js'): The path of the provided scope ('/') is not under the max scope allowed ('/scripts/user/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.
有关详细信息,请参阅 关于 Whosebug 的回答
我尝试使用 JavaScript 为我的网络应用程序创建一个简单的通知功能。为此,我尝试注册一个服务人员,它将发送通知以支持通知 API 之类的 action
.
这是我的代码:
1 navigator.serviceWorker.register('/scripts/dummy.js');
2
3 function notify(title, data) {
4 console.log('function call');
5 if (Notification.permission == 'granted' && defaultStatus != 'dnd') {
6 console.log('permission and not dnd');
7 navigator.serviceWorker.ready.then(function(registration) {
8 console.log('ready, send notification);
9 registration.showNotification(title, data);
10 });
11 }
12 }
问题出在navigator.serviceWorker.ready
Chrome 开发者控制台显示 this。我需要在 dummy.js
中添加一些东西吗? (目前是空的,但我认为它以前有效)
我解决了我的问题。它不起作用的原因是因为我将 dummy.js
从当前目录移动到子目录中。
这是我在将选项 { scope: '/' }
添加到 serviceWorker.register
函数后从 Chrome 得到的错误:
The path of the provided scope ('/') is not under the max scope allowed ('/scripts/user/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.
(index):1 Uncaught (in promise) DOMException: Failed to register a ServiceWorker for scope ('https://mydomain/') with script ('https://mydomain/scripts/user/user_worker.js'): The path of the provided scope ('/') is not under the max scope allowed ('/scripts/user/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.
有关详细信息,请参阅