6

I have a status badge image that returns the HTTP code 503 when the respective service is offline (but the webserver is still there serving calls). Now opening the image URL directly will display the image properly, regardless of the underlying 503 error code. But using it inside an <img> tag shows the broken image icon. How can I prevent that while still allowing the image itself to return a 503? (External services depend on that)

Here are some screenshots to illustrate what's going on:

The badge on the page:
The badge on the page

The status message in the developer console:
The status message in the developer console

The badge itself:
The badge itself

Note: This happens on Firefox. Not Chrome

Edit: Here are a few requested pieces information:

  • Firefox 78.0.2 (64-Bit)
  • It's served from the same domain. But the domain is essentially just proxying serveral underlying webservices. And this badge is originating from a different service but all on the same domain.
  • It's a SVG image if that makes any difference.
BrainStone
  • 3,028
  • 6
  • 32
  • 59
  • Can you provide more information? Where does that badge come from? Is the image served through a different domain name? Which version of Firefox are you using? I just set up a scenario with two images (200 & 503 http status codes) and both are displayed. – Carlos Jiménez Jul 15 '20 at 08:05
  • @CarlosJiménez I updated my question. And make sure that you include the images through an ```` tag. Just opening the image itself works for me too. – BrainStone Jul 15 '20 at 08:09
  • I think firefox maybe prevent to load image from your specific webpage, or maybe some addons blocked it.Check this link: https://support.mozilla.org/en-US/kb/fix-problems-images-not-show. Hope it would help you – Thien Huynh Jul 20 '20 at 05:56
  • @BrainStone Check to make sure the returned SVG or the image tag has a defined width. In Firefox svg in an image tag will not display if the SVG, the img tag, or the ancestors of those tags do not have an explicit width. CSS width is ignored. – NeoVance Jul 22 '20 at 00:53

2 Answers2

2

Since XMLHttpRequest can retrieve the output of any request, no matter the response code, it is possible to request for the image with XMLHttpRequest, and then convert the blob response type to a base64 format image, which can be loaded in the browser.

The CORS proxy I used in the sample code may not be necessary in the majority of cases, but could be useful in the case where the image you are trying to display has weird response headers that prevent access to the image from another domain.

Here is the sample code. It should work no matter the response code, CORS, etc.

var xhr = new XMLHttpRequest();

xhr.onload = function () {
    var reader = new FileReader();
    reader.onloadend = function () {
        // here, reader.result contains the base64-formatted string you can use to set the src attribute with
        document.getElementsByTagName('img')[0].src = reader.result; // sets the first <img> tag to display the image, change to the element you want to use
    };
    reader.readAsDataURL(xhr.response);
};

xhr.open('GET', "https://cors-anywhere.herokuapp.com/i.stack.imgur.com/8wB1j.png"); // don't include the HTTP/HTTPS protocol in the url
xhr.responseType = 'blob';
xhr.setRequestHeader('X-Requested-With', 'xhr');
xhr.send();
<img src="about:blank">

Everything works, as when you go into Inspect Element, you see that the src attribute of the <img> tag points to a base64 URL that can load in any browser.

idontknow
  • 438
  • 5
  • 16
  • Now while I'm not happy that this is a workaround (and a JS based one too), it is the best answer so far. – BrainStone Jul 22 '20 at 13:39
  • Yea, I'm sorry about that. I did want to mention that it is stupidly impractical, but at least it's a working hack that you can use. There might be a better, long-term solution tho; just gotta keep waiting for 'em. – idontknow Jul 22 '20 at 21:26
  • @BrainStone would using an image proxy be an option? I'm thinking either weserv or allorigins https://images.weserv.nl/ http://allorigins.win/ – idontknow Dec 03 '20 at 19:21
-1

You might want to compress or resize your images before uploading it to server , as they might be large enough to keep the server busy and show the error as most of the time, a 503 error occurs because the server is too busy. More over the image is SVG so it might render dimesions before completing, hence I'd suggest

  • Try replacing the SVG with PNG or JPG
  • Also try for site like https://tinypng.com/ to compress the image size

This might work for you

Karan Tewari
  • 498
  • 8
  • 20
  • As mentioned in the question the 503 is intentional. The web server is working fine. The underlying service however is not. Hence the status. – BrainStone Jul 21 '20 at 22:42