【发布时间】:2016-10-12 02:34:23
【问题描述】:
我正在将我们的一个网站转变为 PWA。它有很多来自 S3 的图像失败并出现以下控制台警告:
The FetchEvent for "[image url]" resulted in a network error response: an object that was not a Response was passed to respondWith().
这是软件代码:
self.addEventListener('fetch', event => {
function onFetch (event) {
// Determine type of asset
var request = event.request,
acceptHeader = request.headers.get('Accept'),
resourceType = 'static';
if(acceptHeader.indexOf('text/html') !== -1) {
resourceType = 'content';
} else if(acceptHeader.indexOf('image') !== -1) {
resourceType = 'image';
}
// Network first for HTML and images
if(resourceType === 'content' || resourceType === 'image') {
event.respondWith(fetch(request)
.then(response => addToCache(request, response)) // read through caching
.catch(() => fetchFromCache(event))
.catch(() => offlineResponse(resourceType))
)
}
}
onFetch(event);
});
function addToCache(request, response) {
if(response.ok) { // only 200s
var copy = response.clone(); // Because responses can only be used once
caches.open(cacheName)
.then(cache => {
cache.put(request, copy);
});
return response;
}
}
function fetchFromCache (event) {
return caches.match(event.request)
.then(response => {
if(!response) {
// A synchronous error that will kick off the catch handler
throw Error('${event.request.url} not found in cache');
}
return response;
});
}
这只发生在来自 S3 的图像上,所有其他图像都按预期工作。有谁知道怎么回事?
【问题讨论】: