Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force browser to re-read cached images

Tags:

css

caching

.button { background: url(../Images/button.png); }

Problem: for performance reason all static content has expiration headers and is cached by browser. When image changes user must refresh cache (Ctrl+F5 in IE). I want images to be cached, but when necessary they must be automatically reloaded.

Question: is next approach 'valid'?

.button {
    background: url(../Images/button.png?v=1234);
}

where v=1234 is version of my site. I do not know whether it is 100% valid to write such things in CSS and I do want browsers to still cache images if version is the same. Do all modern browsers correctly cache data with URL parameters part?

Thanks.

like image 708
Roman Avatar asked Sep 09 '09 12:09

Roman


People also ask

How do I force a browser to reload cached files?

To ensure you see the latest version of a site you need to clear the cache memory. This is done by doing a force refresh by pressing both control and F5 buttons simultaneously on your keyboard (depending on your browser). Most times a simple force cache refresh won't work and you need to clear the cache by hand.

How do I view photos in cache?

Hold down the Alt (Option) key. You'll see the Library folder show up in the drop-down menu. Find the Caches folder and then your browser's folder to see all the cached files stored on your computer.

How would you reload a cached CSS file that has changed since it was cached?

In Chrome, you can do a hard reload by opening the console (F12 key) and then right-clicking on the reload button to get a menu with additional reload options. Debugging a live site is rarely a good idea. Cache doesn't throw anything good into the mix either.


1 Answers

That looks like a good approach to me, it'll work fine in CSS in modern browsers - the browser will look at the address of the image (including the ?v=1234), see that it's not cached, and send a fresh request.

like image 192
Dominic Rodger Avatar answered Oct 04 '22 21:10

Dominic Rodger