最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Modifying responses in service worker requests to be an image - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Mar 18, 2022 at 10:07 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Nov 12, 2017 at 14:36 mazlormazlor 1,8954 gold badges20 silver badges20 bronze badges 2
  • 2 I think what's actually happening is that the content of the response is the text "/imgs/sunset.gif" and not the actual gif. You will need to create a Blob with the image data and pass that as the first argument to the Response constructor. – schroffl Commented Nov 12, 2017 at 15:49
  • I second schroffl. The first argument should be a Blob, an ArrayBuffer, or TypedArray. – Duco Commented Mar 25, 2021 at 5:54
Add a ment  | 

3 Answers 3

Reset to default 5

You need to replace your line :

return new Response('/imgs/sunset.gif', {headers:{'Content-Type':'image/gif'}});

By :

return new Response("<img src='/imgs/sunset.gif'/>", {headers:{'Content-Type': 'text/html'}});

You want to display a gif in your page, so you have to add a html content with and tell its source src.

Or easier solution you can only write this :

return fetch('/imgs/sunset.gif');

You can use fetch to get image or gif from files like below.

self.addEventListener('fetch', function(event) {
  event.respondWith(
    fetch(event.request).then(function(response) {
      if (response.status === 404) {           
        return fetch("/path/to/404error.gif");
      }
      return response;
    }).catch(function() {
      return new Response("Uh oh, that totally failed!");
    })
  );
});

I was looking for a similar solution and the following works:

self.addEventListener('fetch', function(event) {
  event.respondWith(
    fetch(event.request).then(function(response) {
      if (response.status === 404) {
        return fetch('/imgs/sunset.gif');
      }
      return response;
    }).catch(function() {
      return new Response("Uh oh, that totally failed!");
    })
  );
});
发布评论

评论列表(0)

  1. 暂无评论