gpt4 book ai didi

javascript - 错误: Request object that has already been used

转载 作者:太空宇宙 更新时间:2023-11-04 02:45:14 25 4
gpt4 key购买 nike

我不断在控制台日志中收到此错误

未捕获( promise 中)类型错误:无法在“ServiceWorkerGlobalScope”上执行“fetch”:无法使用已使用的请求对象构造请求。

我尝试更改我的 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 不允许您使用一个请求两次,至少在当前版本中是这样:)。在 checkResponseaddToCache 中使用相同的请求对象可能是这种情况。您可以在调用 fetch 之前尝试克隆请求对象,如此处所述 Why does this code fail to execute 'fetch'?

关于javascript - 错误: Request object that has already been used,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55979921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com