admin管理员组文章数量:1024727
if ('serviceWorker' in navigator) {
console.log('Service Worker is supported');
navigator.serviceWorker.register('sw.js').then(function(reg) {
console.log(reg);
reg.pushManager.subscribe({
userVisibleOnly: true
}).then(function(sub) {
var endpoint = sub.endpoint;
console.log('endpoint:', sub.endpoint);
});
}).catch(function(err) {
console.log(':(', err);
});
}
This is my simple example of a serviceWorker registration. When I load the page for the first time, I get the error:
Uncaught (in promise) DOMException: Subscription failed - no active Service Worker
If I reload this page all goes fine. In Mozilla I don't have this bug. Why can't the serviceWorker be registered in the first onload?
if ('serviceWorker' in navigator) {
console.log('Service Worker is supported');
navigator.serviceWorker.register('sw.js').then(function(reg) {
console.log(reg);
reg.pushManager.subscribe({
userVisibleOnly: true
}).then(function(sub) {
var endpoint = sub.endpoint;
console.log('endpoint:', sub.endpoint);
});
}).catch(function(err) {
console.log(':(', err);
});
}
This is my simple example of a serviceWorker registration. When I load the page for the first time, I get the error:
Uncaught (in promise) DOMException: Subscription failed - no active Service Worker
If I reload this page all goes fine. In Mozilla I don't have this bug. Why can't the serviceWorker be registered in the first onload?
Share Improve this question edited Apr 28, 2016 at 13:34 gar 14.6k5 gold badges33 silver badges32 bronze badges asked Apr 28, 2016 at 13:29 Константин БелкинКонстантин Белкин 431 silver badge5 bronze badges 01 Answer
Reset to default 6In order to receive push events, you need a successfully installed service worker. You're asking for the subscription before a service worker has been successfully installed, so it fails. Otherwise it would appear like your push subscription worked, but the registration (and therefore your subscription) get thrown away if the only worker fails to install.
navigator.serviceWorker.ready
is a promise that resolves once there's a registration with an active (sucessfully installed) worker. It resolves with the registration, so it works well here.
navigator.serviceWorker.ready.then(reg => reg.pushManager.subscribe(…))
if ('serviceWorker' in navigator) {
console.log('Service Worker is supported');
navigator.serviceWorker.register('sw.js').then(function(reg) {
console.log(reg);
reg.pushManager.subscribe({
userVisibleOnly: true
}).then(function(sub) {
var endpoint = sub.endpoint;
console.log('endpoint:', sub.endpoint);
});
}).catch(function(err) {
console.log(':(', err);
});
}
This is my simple example of a serviceWorker registration. When I load the page for the first time, I get the error:
Uncaught (in promise) DOMException: Subscription failed - no active Service Worker
If I reload this page all goes fine. In Mozilla I don't have this bug. Why can't the serviceWorker be registered in the first onload?
if ('serviceWorker' in navigator) {
console.log('Service Worker is supported');
navigator.serviceWorker.register('sw.js').then(function(reg) {
console.log(reg);
reg.pushManager.subscribe({
userVisibleOnly: true
}).then(function(sub) {
var endpoint = sub.endpoint;
console.log('endpoint:', sub.endpoint);
});
}).catch(function(err) {
console.log(':(', err);
});
}
This is my simple example of a serviceWorker registration. When I load the page for the first time, I get the error:
Uncaught (in promise) DOMException: Subscription failed - no active Service Worker
If I reload this page all goes fine. In Mozilla I don't have this bug. Why can't the serviceWorker be registered in the first onload?
Share Improve this question edited Apr 28, 2016 at 13:34 gar 14.6k5 gold badges33 silver badges32 bronze badges asked Apr 28, 2016 at 13:29 Константин БелкинКонстантин Белкин 431 silver badge5 bronze badges 01 Answer
Reset to default 6In order to receive push events, you need a successfully installed service worker. You're asking for the subscription before a service worker has been successfully installed, so it fails. Otherwise it would appear like your push subscription worked, but the registration (and therefore your subscription) get thrown away if the only worker fails to install.
navigator.serviceWorker.ready
is a promise that resolves once there's a registration with an active (sucessfully installed) worker. It resolves with the registration, so it works well here.
navigator.serviceWorker.ready.then(reg => reg.pushManager.subscribe(…))
本文标签: javascriptserviceworker does not load the first timeStack Overflow
版权声明:本文标题:javascript - serviceworker does not load the first time - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745622197a2159642.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论