gpt4 book ai didi

javascript - 服务 worker 的事件监听器放在哪里

转载 作者:行者123 更新时间:2023-11-29 16:00:15 25 4
gpt4 key购买 nike

基本上,我试图在检测到新的 serviceworker 版本时显示一个对话框,然后让用户决定重新加载以获取它。为此,我们需要在重新加载窗口之前主动设置 skipWaiting

这是我的操作:

  onClickHandler = () => {
console.log('on click', 'posting skipWaiting');
navigator.serviceWorker.controller.postMessage('skipWaiting');
};

这是我创建 eventListener 的尝试:

navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
console.log('worker state', installingWorker.state);
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed.'
);

navigator.serviceWorker.addEventListener('message', event => {
console.log('skip waiting');
if (event.data === 'skipWaiting') {
self.skipWaiting();
}
});
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
});

问题是 navigator.serviceWorker.addEventListener('message', event => 没有被触发。我声明的监听器是错误的吗?

最佳答案

你很接近。在您安装的 block 中,您可以检查

navigator.serviceWorker.controller

如果存在,则意味着旧内容将被清除,新内容将被添加到缓存中。现在是显示消息或强制刷新的最佳时机。

  navigator.serviceWorker.register('service-worker.js').then(function (registration) {

$log.debug('The service worker has been registered ', registration);

if(navigator.online) {
toastr.warning('Offline Mode', 'Application Status');
}

// updatefound is fired if service-worker.js changes.
registration.onupdatefound = function () {
// The updatefound event implies that reg.installing is set; see
// https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-container-updatefound-event
var installingWorker = registration.installing;

installingWorker.onstatechange = function () {
switch (installingWorker.state) {
case 'installed':
if (navigator.serviceWorker.controller) {
// At this point, the old content will have been purged and the fresh content will
// have been added to the cache.
// It's the perfect time to display a "New content is available; please refresh."
// message in the page's interface.
toastr.info('Application has been updated, please refresh this page for the most recent version');

window.location.reload();

});
caches.delete('scope-dynamic').then(function () {
$log.debug('Killed the dynamic cache!');
})
$log.debug('New or updated content is available.');
} else {
// At this point, everything has been precached.
// It's the perfect time to display a "Content is cached for offline use." message.
toastr.success('Content is cached for offline use.', 'Application Status');
$log.debug('Content is now available offline!');
}
break;

case 'redundant':
$log.error('The installing service worker became redundant.');
break;

}
};
};
}).catch(function (e) {
$log.error('Error during service worker registration:', e);
});

那里散落着一些棱 Angular 分明的东西,但这应该可以帮助你到达你想去的地方。

关于javascript - 服务 worker 的事件监听器放在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54258089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com