Hello.
There is a technique called "Ghost Querystring" that solves this problem in most browsers.
In PHP it is done like this:
<link href="/file-css.css?<?php echo time(); ?>" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/file-javascript.js?<?php echo time(); ?>"></script>
Rendered:
<link href="/file-css.css?1433981258" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/file-javascript.js?1433981258"></script>
This generates a different URL in each "F5" and forces the browser to re-download the file.
This technique is used to avoid having to change the web server header settings.
Updating
The above solution can cause two problems:
1º) The increase of the internet traffic of the web server.
2º) The web application loads more slowly because it does not use the benefits of browser caching.
So to solve these two problems there are two solutions.
first solution:
Create a constant that would only be updated when CSS and JS files change version.
In ASP.NET:
I put a constant on the Web.config that indicates the version:
<appSettings>
<add key="Version" value="1254"/>
</appSettings>
On the Site.Master I put:
<link href="/file-css.css?<%= ConfigurationManager.AppSettings["Version"] %>" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/file-javascript.js?<%= ConfigurationManager.AppSettings["Version"] %>"></script>
In PHP:
It is recommended that the constant "VERSION" be created in a file of type "config.php". But I put it here only for demonstration:
<?php
define('VERSION', '1254');
?>
<link href="/file-css.css?<?php echo VERSION; ?>" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/file-javascript.js?<?php echo VERSION; ?>"></script>
The two programming languages generate the same result in HTML:
<link href="/file-css.css?1254" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/file-javascript.js?1254"></script>
second solution
Write the HTML itself by changing the numbering "1254" only when the CSS and JS files change version.
This last solution I consider the easiest.
References: Prevent Your CSS and Javascript Files From Being Cached
Have you checked the META CACHE-CONTROL and EXPIRE? http://www.i18nguy.com/markup/metatags.html
– Maicon Carraro
I’ve seen the
Cache-Control
, but as far as I know only allows me to disable the cache, which I do not want. Already theExpires
serves for search robots to withdraw them from their results when the date "expires".– Marcus Vinicius
Got it, about the version in the javascript file name you know you don’t necessarily have to be in the file name right? It might be something like
seuarquivo.js?v=2
– Maicon Carraro
Yes, I had already seen this solution, but I gave it up because I read somewhere that the browsers were ignoring parameters of query string in archives
.js
. Can anyone there confirm that this solution is still viable and possible?– Marcus Vinicius
@Marcusvinicius may be that this give you an idea. We use
query params
to clean up the cache and has functioned smoothly so far.– Omni
Can generate a
time()
and put in the URL too.– Diego Souza
Hello, So I checked I usually added a query string in the urls of the files as link : http://stackoverflow.com/questions/32414/how-can-i-force-clients-to-refresh-javascript-files http://stackoverflow.com/questions/15936050/force-browser-to-reload-all-cache-after-site-update http://blog.toddbauer.me/2013/01/30/forcing-browsers-to-reload-css-js-files/ This technique works because even if the file is cached if it is sent with a different querystring the browser interprets as if it were another file and performs the Reload.
– Luã Govinda Mendes Souza