Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh CSS file in cache with ?version=XXX

In order to refresh a CSS file in cache, I often use the file.css?version=DDMMYYYY trick. However, I have a question about this method.

I'm working on an old extranet. All pages are using the same CSS file. The extranet doesn't have any template file and this css is included in every page of the extranet. I need to refresh this css file for all the extranet pages.

My question is : I want to use the file.css?version=DDMMYYYY trick on the login page. The other pages will still include file.css (without the ?version part)

If the user come on the login page, he will receive the new version of the css file. But which version will be used on the other pages? The old version (file.css) or the new version (file.css?version=DDMMYYYY) ?

In other words, when the user come on the login page, which files will be in his cache :

  • file.css and file.css?version=DDMMYYYY
  • only file.css, updated to the new version

I'm sorry for this beginner question but I have some difficulties to test it myself.

Thanks for your help!

like image 699
Guigoz Avatar asked Dec 20 '12 16:12

Guigoz


People also ask

How do I refresh the CSS cache?

General solution Pressing Ctrl + F5 (or Ctrl + Shift + R ) to force a cache reload.

Can CSS be cached?

However, CSS caching can also cause problems for both users and developers. If you change a stylesheet, any given user of your site may be unable to see the changes until their browser cache clears—that is, until it deletes the saved version and fetches the stylesheet from scratch again.

How do I refresh CSS in Chrome?

For Windows in Chrome or Edge, the keyboard shortcut Ctrl + F5 (or Ctrl + Reload) refreshes. For Mac, hold Cmd-Shift-R or Shift-Reload. Most browsers also have a refresh button next to the URL.

How do I force the browser to reload cached CSS and JavaScript files?

It works, all the time, for everyone. But, if you're not using it, and you just need to reload that one CSS or JS file occasionally in your own browser... just open it in its own tab and hit SHIFT-reload (or CTRL-F5)!


2 Answers

When the file gets cached, it will be with the full url including the ? and stuff after it. The caching headers are supplied by the server and obeyed by the browser.

Essentially

file.css?version=DDMMYYYY
and
file.css

Are 2 separate files for the browser, with no connection what so ever.

My suggestion to you would be to use the new url consistently on all pages.

like image 84
closure Avatar answered Oct 18 '22 22:10

closure


This will not work, they will be cached differently, although file.css and file.css?version=DDMMYYYY are the same file in the filesystem, they are different URIs and the server response can be totally different... So, file.css could load the old file from cache at the same time that you get the correct file with file.css?version=DDMMYYYY.

A way to prevent this could be totally disabling the cache, this would cause the css file to be download every time you load a page, but would give you instantaneous update, or you could set the cache to expire in short time, like 10 minutes, 2 hours, so it would be downloaded once every 10 min/2 hours...

If you are using apache with htaccess enabled you could do this to disable the cache:

<filesMatch ".(html|htm|js|css)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>
like image 29
José Roberto Araújo Júnior Avatar answered Oct 18 '22 22:10

José Roberto Araújo Júnior