最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firefox TypeError: ServiceWorker script encountered an error during installation - Stack Overflow

programmeradmin3浏览0评论

I'm developing web push notification on my website. I follow the Web Push Notifications of Google and The Service Worker Cookbook of Mozilla.

I have tested on the Google Chrome v50+ and everything is working but I will get the error below on Firefox 44, 45, 46, 52, latest Firefox (version 57.0.4 64 bit) when calling navigator.serviceWorker.register('./push-service-worker.js') function.

TypeError: ServiceWorker script at http://localhost:9600/push-service-worker.js for scope http://localhost:9600/ encountered an error during installation.

This is my code:

Register ServiceWorker in controller.js

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);
});

push-service-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);
        }
      })
  );
});

Directory structure

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

Thank for helping!

I'm developing web push notification on my website. I follow the Web Push Notifications of Google and The Service Worker Cookbook of Mozilla.

I have tested on the Google Chrome v50+ and everything is working but I will get the error below on Firefox 44, 45, 46, 52, latest Firefox (version 57.0.4 64 bit) when calling navigator.serviceWorker.register('./push-service-worker.js') function.

TypeError: ServiceWorker script at http://localhost:9600/push-service-worker.js for scope http://localhost:9600/ encountered an error during installation.

This is my code:

Register ServiceWorker in controller.js

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);
});

push-service-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);
        }
      })
  );
});

Directory structure

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

Thank for helping!

Share Improve this question edited Jan 18, 2018 at 1:10 Tuan Nguyen asked Jan 17, 2018 at 7:29 Tuan NguyenTuan Nguyen 2691 gold badge3 silver badges12 bronze badges 0
Add a comment  | 

5 Answers 5

Reset to default 3

As wanderview said at 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.

This problem is encountered when I use one profile for multiple Firefox version. To fix this issue go to about:support and click Refresh Firefox. If it doesn't work, you can go to about:profiles, click Create new profile, and then Launch profile in new browser.

In my case, this was caused by Firefox not being able to access the serviceworker.js file.

The server I was hosting it on had a permissions check based on cookies, and in this case Firefox was not sending the cookie as I believe it was considered a cross-site script.

On the server, I made the serviceworker.js file accessible publicly, and then Firefox could register the service worker.

It was particularly difficult to notice this, because the Firefox Developer Tools did not show the Forbidden response in the Console, nor did it even show any request for serviceworker.js in the Network tab.

Therefore, presumably the TypeError is in fact a generic error and should be read as 'something went wrong registering the Service Worker'.

this error can be caused by the service worker file not being reachable or not existing

as per @tbigby Answer, I moved my serviceworker.js file to 'assets' folder and was able to register service worker successfully.

Firefox did this to me because Typescript generated an empty export {};. I have type: "module" in the service worker registration options. I suspect this is a bug in Firefox.

发布评论

评论列表(0)

  1. 暂无评论