When Multiple Instances of Same Images Are Embedded in an HTML, Does That Load The Image Once

When multiple instances of same images are embedded in an HTML, does that load the image once?

Try it — when looking into caching issues, a tool like Firebug for Firefox or the Developer Tools within Chrome are very beneficial. If you open the 'Net' panel in either and reload a page, you will see what HTTP status code was sent for each item. 304 (Not modified) means that the item was retrieved locally from the cache.

As dthorpe says above, cache headers are important here. As well as making sure that 'no-cache' hasn't been set, if you have access to your server configuration you should be pro-active — if you know a resource isn't going to change you should make sure to set either an 'Expires' header (which tells browsers a date after which the cached copy should be considered stale) or a 'Cache-Control: max-age' header (which gives a number of days/hours rather than a set date).

You can set different time-scales for different mime-types/folders too, which allows you to get clients data to refresh HTML content often, but images and stylesheets rarely.

Here's a nice intro video/article by Google that's worth checking out.

Will browser load the same image file if it's called multiple times in the same page?

Default behavior for most browsers is to cache images on first load. Multiple calls for same image would be served from the cache.

If you wish to disallow browser caching read more here: How to control web page caching, across all browsers?

Different HTML pages using the same image, does the browser fetch the image once and use it twice?

It depends on the caching headers the server sends out alongside the image.

There are several ways in which caching can be done.

When a resource is served with an expiry date in the future, the browser will use its cached copy until that date is reached (or the cache is emptied or a refresh forced.

Another way is for the server to listen to the If-modified-since request header. The server can then check whether the resource has been modified since that date. If it hasn't, it will return a 304 not modified status; otherwise, the updated resource.

The Apache Caching guide is a (lengthy) introduction to the subject.

To see in your browser what caching rules apply, open its developer tools and look for the "Net" tab. It's a list of all the requests that were made in connection with the current page. It'll tell you whether a resource was loaded from the server, or a cached copy used.

For example, this result on a Stack Overflow image from Chrome's developer tools:

Sample Image

suggests the image was cached. When I click that row, I can switch to the "header" view, where I can see the exact caching instructions the server sends:

Sample Image

It means that as long as the browser has a cached copy of the image, it will keep on using that without ever checking with the server until December 17, 2014.

Same CSS background specified multiple times

Actually in all the browsers, we have browser cache. Once it is loaded it will not be loaded again. If the file size and names are same, there is no way it is going to be loaded again. You can track that using firebug or developertools.

Try it — when looking into caching issues, a tool like Firebug for
Firefox or the Developer Tools within Chrome are very beneficial. If
you open the 'Net' panel in either and reload a page, you will see
what HTTP status code was sent for each item. 304 (Not modified) means
that the item was retrieved locally from the cache.

As dthorpe says above, cache headers are important here. As well as
making sure that 'no-cache' hasn't been set, if you have access to
your server configuration you should be pro-active — if you know a
resource isn't going to change you should make sure to set either an
'Expires' header (which tells browsers a date after which the cached
copy should be considered stale) or a 'Cache-Control: max-age' header
(which gives a number of days/hours rather than a set date).

You can set different time-scales for different mime-types/folders
too, which allows you to get clients data to refresh HTML content
often, but images and stylesheets rarely.

Here's a nice intro video/article by Google that's worth checking out.

Highlighted are the proper answers from this stackoverfow question. It will be interesting for you if you try to know about how web browsers work.



Related Topics



Leave a reply



Submit