微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Firefox TypeError:ServiceWorker脚本在安装过程中遇到错误

我正在我的网站上开发网络推送通知.我关注了Google的Web Push Notifications和Mozilla的The Service Worker Cookbook.

我已经在Google Chrome v50上进行了测试,一切正常,但在调用navigator.serviceWorker.register(‘./时,我会在Firefox 44,45,46,52,最新的Firefox(版本57.0.4 64位)上收到以下错误消息. push-service-worker.js’)函数.

TypeError: ServiceWorker script at 07002 for scope 07003 encountered an error during installation.

这是我的代码

在controller.js中注册ServiceWorker

navigator.serviceWorker.register('push-service-worker.js')
.then((registration) => {
  return registration.pushManager.getSubscription()
    .then((subscription) => {
      if (subscription) {
        return subscription;
      }
      var subscribeOptions = {
        userVisibleOnly: true,applicationServerKey: buildApplicationServerKey(),};
      return registration.pushManager.subscribe(subscribeOptions);
    });
})
.then((subscription) => {
  sendSubscriptionToServer(subscription);
})
.catch((err) => {
  console.log('Unable to subscribe to push: ',err);
});

推送服务worker.js

'use strict';

self.addEventListener('push',(event) => {
  var payload = event.data.json();
  var title = payload.title || 'Title';
  event.waitUntil(
    self.registration.showNotification(title,{
      body: payload.body,icon: './common/images/notification-icon-192x192.png',image: payload.image || '',})
  );
});

self.addEventListener('notificationclick',(event) => {
  event.notification.close();
  var urlToOpen = new URL('/',self.location.origin).href;
  event.waitUntil(
    clients.matchAll({
      type: 'window',includeUncontrolled: true,})
      .then((windowClients) => {
        var matchingClient = null;
        for (var i = 0; i < windowClients.length; i++) {
          var windowClient = windowClients[i];
          if (windowClient.url === urlToOpen) {
            matchingClient = windowClient;
            break;
          }
        }

        if (matchingClient) {
          return matchingClient.focus();
        } else {
          return clients.openWindow(urlToOpen);
        }
      })
  );
});

目录结构

./root
  ---- manifest.json
  ---- push-service-worker.js
  ---- src
       ---- controller.js

谢谢你的帮助!

最佳答案
正如wanderview在here所说:

FWIW,you should always use a separate profile for each channel (release/beta/dev-edition/nightly). We’re working on making it work like that out-of-the-Box,but its not ready yet.

当我为多个Firefox版本使用一个配置文件时遇到此问题.要解决此问题,请转到about:support并单击Refresh Firefox.如果它不起作用,您可以转到about:profiles,单击Create new profile,然后在新浏览器中启动profile.

原文地址:https://www.jb51.cc/js/429244.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐