Hello and thank you in advance.
My question is about responding to a network request using ServiceWorker. I am able to handle it in case of a text or html, but when I am trying to deal with an image I am failing, here is the code I have:
self.addEventListener('fetch', function(event){
event.respondWith(
fetch(event.request).then(function(response){
if(response.status === 404){
return new Response('The page wasn\'t found');
}
return response;
}).catch(function(){
return new Response('The network is totally failed');
})
);
});
The snippet above is working when dealing with text and html but when I am trying with an image it brings a totally black screen. Here what I used but it didn't work.
self.addEventListener('fetch', function(event){
event.respondWith(
fetch(event.request).then(function(response){
if(response.status === 404){
return new Response('/imgs/sunset.gif', {headers:{'Content-Type':'image/gif'}});
}
return response;
}).catch(function(){
return new Response('The network is totally failed');
})
);
});
Hopefully you can help me in knowing what I am missing. thanks.