Firefox TypeError: ServiceWorker script encountered an error during installation
Asked Answered
R

5

13

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!

Riggle answered 17/1, 2018 at 7:29 Comment(0)
R
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.

Riggle answered 1/3, 2018 at 1:35 Comment(0)
D
2

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'.

Divine answered 13/12, 2022 at 20:45 Comment(0)
T
0

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

Tellurize answered 23/3, 2023 at 16:53 Comment(0)
A
0

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

Arnst answered 2/8, 2023 at 11:52 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewAlcinia
T
0

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.

Tautologism answered 21/11, 2023 at 0:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.