Service Worker 注册失败 [英] Service Worker Registration Failed

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

问题描述

我目前正在使用 Service Worker 来处理浏览器中的推送通知.目前我遇到了这个软件注册失败错误":

I am currently working on service worker to handle push notification in browser. Currently I am having this "SW registration failed error":

软件注册失败,错误为 SecurityError: Failed to register a ServiceWorker: The URL protocol of the current origin ('null') is not supported.

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

检查下面的client1.htmlservice-worker.js文件:

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>

有人可以帮忙解决这个问题吗?

Can anyone help with this issue?

推荐答案

已解决:首先是 Service Worker 只能在 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){

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

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