我不断收到此错误:
未捕获(承诺中)DOMException:无法在“FetchEvent”上执行“respondWith”:该事件已得到响应。
我知道如果 fetch 函数中发生异步操作,服务人员会自动响应,但我无法确定这段代码中的哪一位是违规者:
importScripts('cache-polyfill.js');
self.addEventListener('fetch', function(event) {
var location = self.location;
console.log("loc", location)
self.clients.matchAll({includeUncontrolled: true}).then(clients => {
for (const client of clients) {
const clientUrl = new URL(client.url);
console.log("SO", clientUrl);
if(clientUrl.searchParams.get("url") != undefined && clientUrl.searchParams.get("url") != '') {
location = client.url;
}
}
console.log("loc2", location)
var url = new URL(location).searchParams.get('url').toString();
console.log(event.request.hostname);
var toRequest = event.request.url;
console.log("Req:", toRequest);
var parser2 = new URL(location);
var parser3 = new URL(url);
var parser = new URL(toRequest);
console.log("if",parser.host,parser2.host,parser.host === parser2.host);
if(parser.host === parser2.host) {
toRequest = toRequest.replace('https://booligoosh.github.io',parser3.protocol + '//' + parser3.host);
console.log("ifdone",toRequest);
}
console.log("toRequest:",toRequest);
event.respondWith(httpGet('https://cors-anywhere.herokuapp.com/' + toRequest));
});
});
function httpGet(theUrl) {
/*var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;*/
return(fetch(theUrl));
}
任何帮助,将不胜感激。