Javascript 服务工人';s';获取';:不是响应的对象已传递给respondWith()

Javascript 服务工人';s';获取';:不是响应的对象已传递给respondWith(),javascript,async-await,service-worker,Javascript,Async Await,Service Worker,服务工作者“获取”未返回响应对象 我正在尝试使用缓存内容回答请求。如果缓存没有所需的内容,我将从服务器获取,将响应放入缓存并返回 self.addEventListener('fetch',函数(事件){ if(event.request.method!=“GET”) 返回; event.waitill((异步()=>{ //一些IndexedDB的东西很好用 })()); event.respondWith((async()=>{/是的,在async函数内部,最好使用wait而不是.then(

服务工作者“获取”未返回响应对象

我正在尝试使用缓存内容回答请求。如果缓存没有所需的内容,我将从服务器获取,将响应放入缓存并返回

self.addEventListener('fetch',函数(事件){
if(event.request.method!=“GET”)
返回;
event.waitill((异步()=>{
//一些IndexedDB的东西很好用
})());

event.respondWith((async()=>{/是的,在
async
函数内部,最好使用
wait
而不是
.then()
链来构造基于承诺的逻辑

这里有一个大致相同的重写。您可能需要根据您所追求的确切行为对某些逻辑进行重新排序:

event.respondWith((异步()=>{
const cachedResponse=等待caches.match(event.request);
如果(缓存响应){
返回缓存响应;
}
const response=等待获取(event.request);
如果(!response | | response.status!==200 | | response.type!=='basic'){
返回响应;
}
如果(启用\u动态\u缓存){
const responseToCache=response.clone();
常量缓存=等待缓存。打开(动态缓存)
等待cache.put(event.request,response.clone());
}
返回响应;
})());