服务工作进程安装事件未触发



从谷歌的页面中汲取灵感,我把它粘贴到我的网站上:

var CACHE_NAME = 'my-site-cache-v1';
var urlsToCache = [
'serviceworker.css'
];
debugger // 1
self.addEventListener('install', function(event) {
debugger // 2
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});

调试器 1 停止程序流,但永远不会到达调试器 2。

服务工作者.css存在。

我正在使用开发人员工具栏打开的隐身窗口导航到该页面。

上面代码段中的代码必须通过寄存器加载。您需要使用 https 进行开发才能看到这项工作

if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('./codeWithYourJsAbove.js').then((function(registration) {
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}), function(err) {
console.log('ServiceWorker registration failed: ', err);
});
});

}

最新更新