Firefox TypeError: ServiceWorker script encountered an error during installation
Firefox TypeError: ServiceWorker script encountered an error during installation
我正在我的网站上开发网络推送通知。我遵循 Mozilla 的 Web Push Notifications of Google and The Service Worker Cookbook。
我已经在 Google Chrome v50+ 上进行了测试,一切正常,但我会在 Firefox 44、45、46、52 和最新的 Firefox(版本 57.0.4 64)上收到以下错误位)调用 navigator.serviceWorker.register('./push-service-worker.js')
函数时。
TypeError: ServiceWorker script at http://localhost:9600/push-service-worker.js for scope http://localhost:9600/ 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
,然后单击 Launch profile in new browser
。
我正在我的网站上开发网络推送通知。我遵循 Mozilla 的 Web Push Notifications of Google and The Service Worker Cookbook。
我已经在 Google Chrome v50+ 上进行了测试,一切正常,但我会在 Firefox 44、45、46、52 和最新的 Firefox(版本 57.0.4 64)上收到以下错误位)调用 navigator.serviceWorker.register('./push-service-worker.js')
函数时。
TypeError: ServiceWorker script at http://localhost:9600/push-service-worker.js for scope http://localhost:9600/ encountered an error during installation.
这是我的代码:
在controller.js
中注册ServiceWorkernavigator.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
,然后单击 Launch profile in new browser
。