未捕获(承诺)TypeError:无法在“ ServiceWorkerGlobalScope”上执行“获取”:无法使用已使用的Request对象构造Request。
我尝试更换服务人员,但不起作用
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
});
});
};
答案 0 :(得分:0)
fetch
不允许您两次使用请求,至少在当前版本中是这样的:)。可能是在checkResponse
和addToCache
中使用相同的请求对象。您可以尝试在调用fetch
之前克隆请求对象,如此处Why does this code fail to execute 'fetch'?