服务工作者事件侦听器-获取



我已经通过下一个代码创建了一个服务工作者:

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

并得到正确的答案,sw.js中的事件"install"可以发射,但事件"fetch"从不发射。

我使用80(http(端口的http服务器(node-js(,我在DevTools Chrome和控制台中看到了来自http服务器的请求,但我的服务人员没有发出任何请求。

我的服务人员代码:

self.addEventListener('install', () => {
console.log("event - install"); // OK
});
self.addEventListener("fetch", (e) => {
console.log("A"); // BAD
return e.request;
});

我在机身末端中使用此代码

setTimeout(() => {
alert("S");
fetch("/svg.svg", {
method: "GET"
});
}, 10000);

serviceworker必须在https协议中运行,但http服务器创建一个http服务器

好的,我决定我的问题。我忘了删除DevTools(在"网络"选项卡(Chrome中的"禁用缓存"选项,我昨天已经使用了这个选项。

最新更新