我不断在控制台日志中收到此错误
未捕获(承诺中)类型错误:无法在“ServiceWorkerGlobalScope”上执行“获取”:无法使用已使用的请求对象构造请求。
我尝试更改我的 Service Worker 但不起作用
self.addEventListener('install', (event) => event.waitUntil(preLoad()));
const preLoad = function () {
return caches.open('cc-offline').then((cache) => {
return cache.addAll(['/offline.html', '/index.html']);
});
}
self.addEventListener('fetch', (event) => {
event.respondWith(checkResponse(event.request).catch(function () {
return returnFromCache(event.request)
}));
event.waitUntil(addToCache(event.request));
});
const checkResponse = (request) => {
return new Promise((fulfill, reject) => {
fetch(request).then((response) => {
(response.status !== 404) ? fulfill(response) : reject()
}, reject)
});
};
const addToCache = (request) => {
return caches.open('cc-offline').then((cache) => {
return fetch(request).then((response) => {
return cache.put(request, response);
});
});
};
const returnFromCache = (request) => {
return caches.open('cc-offline').then((cache) => {
return cache.match(request).then((matching) => {
return (!matching || matching.status == 404) ? cache.match('offline.html') : matching
});
});
};
fetch
不允许您使用一个请求两次,至少在当前版本中是这样:)。在两者中使用相同的请求对象checkResponse
and addToCache
也许是这样。您可以尝试在调用之前克隆请求对象fetch
正如这里提到的为什么这段代码无法执行“fetch”? https://stackoverflow.com/questions/35394924/why-does-this-code-fail-to-execute-fetch
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)