Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Chrome make a new request for this SVG image each time, but not for the PNG?

I have an app that displays the same image in multiple locations and may change the src of an image.

When I point to a PNG image that I've already used before, the browser does not bother making a new request, it simply uses the PNG image that's already in the cache. However, when I point to an SVG image image that I've used before, the browser (Chrome 22) makes a new request. The server returns 304 (Not Modified), so no new image needs to be downloaded, but this still takes some extra processing.

This can be easily tested in this jsFiddle: http://jsfiddle.net/jtmuw/1/

$('button').click( function() {
  $('#a').attr('src', "myImage.svg");

  $('#b').attr('src', "myImage.png");
});

If you open the fiddle with Chrome (or at least Chrome v.22.0.1229.94) and you open up your network tab you will see the two images have been requested. If you then hit "reload images" several times, your network tab will show multiple requests for the SVG image but no further requests for the PNG image.

As far as I can tell, the same headers are being returned by the server, so I can't see any reason for the difference.

I am not seeing this on FF or Safari, so this may be a Chrome issue. However, I still feel like maybe there is some underlying differences in the headers that I'm missing, and it's not just that Chrome is treating SVG images weirdly.

like image 240
Sam Fen Avatar asked Nov 05 '12 20:11

Sam Fen


People also ask

How do I save a SVG file as a PNG in Chrome?

svg file, right click on it and click on the context menu item 'Save SVG as PNG. Lets you click on the extension icon or right click on an . svg file and choose Save SVG as PNG. You will be able to specify the desired width of the rendered PNG image.

Why are my SVG images not showing?

If you are trying to use SVG like <img src="image. svg"> or as a CSS background-image , and the file is linked to correctly and everything seems right, but the browser isn't displaying it, it might be because your server is serving it with an incorrect content-type.

Why is SVG not showing up in Chrome?

Chrome browser doesn't support SVG format with <img src=””> tag anymore. Peoples are facing issues that they aren't able to see their images in Chrome browser because the images are in SVG format. So I found a solution to display SVG image in Chrome browser with <object> tag.

Are SVGs cached by browser?

The SVG file can be cached by the browser, resulting in faster loading times for any page that uses the image loaded in the future.


2 Answers

You can perhaps force Chrome to cache the file. w3schools has a pretty good overview of this as follows: http://www.w3schools.com/html/html5_app_cache.asp

Essentially you'll want to create a manifest file (call it... "myCache.appcache" or whatever else)

CACHE MANIFEST
/path/to/svg/file.svg

and then point to this in your html file as so:

<html manifest="myCache.appcache">

This will tell Chrome that, even when you don't have internet access, this file should be cached and accessible anyway.

like image 136
Graham Robertson Avatar answered Sep 27 '22 20:09

Graham Robertson


Include the SVG image at the top of the document.

<html>
<head>
    ...
</head>
<body>
    <img style="display:none" src="cached.svg">
    ....
</body>
<html>
like image 21
rbt Avatar answered Sep 27 '22 18:09

rbt