服务人员注册失败 [英] Service Worker Registration Failed

查看:1269
本文介绍了服务人员注册失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正致力于服务工作者在浏览器中处理推送通知。目前我有这个SW注册失败错误。有人可以帮忙解决这个问题吗?检查下面的 client1.html service-worker.js 文件:

I am currently working on service worker to handle push notification in browser. Currently I am having this "SW registration failed error". Can anyone help with this issue? Check client1.html and service-worker.js file below:

错误:


SW注册失败并出现错误SecurityError:无法注册
ServiceWorker:URL协议当前来源('null')不支持

SW registration failed with error SecurityError: Failed to register a ServiceWorker: The URL protocol of the current origin ('null') is not supported.

service-worker.js

console.log('Started', self);
self.addEventListener('install', function(event) {
  self.skipWaiting();
  console.log('Installed', event);
});
self.addEventListener('activate', function(event) {
console.log('Activated', event);
});
self.addEventListener('push', function(event) {
  console.log('Push message received', event);
}); 

client1.html

  <!doctype html>
  <html>
  <head>
  <title>Client 1</title>
  </head>
  <body>
  <script>
     if('serviceWorker' in navigator){
        // Register service worker
        navigator.serviceWorker.register('service-worker.js').then(function(reg){
            console.log("SW registration succeeded. Scope is "+reg.scope);
        }).catch(function(err){
            console.error("SW registration failed with error "+err);
        });
    }
  </script>
  </body>
  </html>


推荐答案

已解决:
首先,服务工作者只能在https或localhost中以安全模式工作。
它不适用于本地资源,如file://或http。

Solved: First thing is service worker only works in secure mode either in https or localhost. It doesnot work in local resources like file:// or http.

,第二个问题是在注册期间。

and second issue was during registration.

     navigator.serviceWorkerContainer.register('service-worker.js').then(function(reg){

这篇关于服务人员注册失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆