javascript tutorial - [Solved-5 Solutions] Reload cached CSS/JS files - javascript - java script - javascript array
Problem:
How to force browser to reload cached CSS/JS files?
Solution 1:
Update: Rewritten to incorporate suggestions from John Millikin and da5id. This solution is written in PHP, but should be easily adapted to other languages.
2: Incorporating comments from Nick Johnson that the original .htaccess
regex can cause problems with files like json-1.3.js.
Solution is to only rewrite if there are exactly 10 digits at the end. (Because 10 digits covers all timestamps from 9/9/2001 to 11/20/2286.)
Solution 2:
First, we use the following rewrite rule in .htaccess:
Now, we write the following PHP function:
Now, wherever you include your CSS, change it from this:
To this:
This way, you never have to modify the link tag again, and the user will always see the latest CSS. The browser will be able to cache the CSS file, but when you make any changes to your CSS the browser will see this as a new URL, so it won't use the cached copy.
Solution 3:
This will ensure that everyone gets the new file. It works because the browser looks at the URL of the file to determine whether it has a copy in cache. If your server isn't set up to do anything with the query string, it will be ignored, but the name will look like a new file to the browser.
On the other hand, if you're developing a website, you don't want to change the version number every time you save a change to your development version. That would be tedious.
So while you're developing your site, a good trick would be to automatically generate a query string parameter:
Solution 4:
Solution 5:
All we need to do if get the file's modified timestamp and append it as a querystring to the file
In PHP i would do it as:
- All we need to do if get the file's modified timestamp and append it as a querystring to the file
- In PHP i would do it as: