-1

I've noticed that many websites has a GET parameter attached to their js and css files. So files become something like: path/to/my.css?v=49 or path/to/my.js?v=1.4.

I did some research and I understood that it used for files caching so the browser won't download the files again every request. I've tried to understand how exactly it works but honestly, I have no idea.

I've seen some posts that says that you tell the browser to download the new file every few minutes, but I was thinking it can be a little bit tricky because if I push a major change (or even a critical bug fix) and the user it trying to access the website but his browser is still using the cached file he won't be able to access it even tho the bug was fixed. He will have to wait until his browser will pull the new file from the server instead of using the cached version. (Obviously I assume that the user does not know when the bug fix will be pushed, when the browser will pull the new files or how to manually pull the new files without waiting for the browser.)

I would like to read about that caching method and how I can overcome the issue I mentioned above.

kfirba
  • 5,231
  • 14
  • 41
  • 70

2 Answers2

4

Adding such a parameter to a file will result in a re-download by the browser. By changing the parameter on a "critical fix" you will achieve that every client re-downloads the file even if cached.

you can force a download on every reload by giving your file a random parameter. Please note that this will increase your traffic and loading times.

<link rel="stylesheet" type="text/css" href="style.css?v=<?php echo rand(); ?>" />
Daniele D
  • 838
  • 1
  • 8
  • 21
  • does it mean that the browser will automatically cache the css/js files and won't download them every request? – kfirba Oct 15 '14 at 11:42
2

It's very simple mechanism. This "v" parametr has to be changed every time you change js or css file.

You can read how to do it automatically here: http://www.codeproject.com/Articles/203288/Automatic-JS-CSS-versioning-to-update-browser-cach

Piotr
  • 146
  • 5
  • Thanks for the answer. By default, for how long the server is going to cache the files from? – kfirba Oct 15 '14 at 11:47
  • there's not any default cache time on the server side, you have to use specific mechanism, but server can for example set header Cache-Control and tell all proxies and browsers what's the max-age of cached response (more here: https://www.mnot.net/cache_docs/#CACHE-CONTROL) – Piotr Oct 15 '14 at 12:06