When I update the css file on the server, many client browsers will continue to load pages using the old cached css file for some time.
After searching for many messages and combining different ideas, I came to what seems the simplest and cheapest.
If any css file is linked, add hrefwith the timestamp the last modified date of the file, for example:
<link rel="stylesheet" type="text/css" href="main.css?t=<?=filemtime('main.css')?>" />
I use CakePHP, so what I do in the layout file:
<? $t = filemtime(CSS . 'main.css'); ?>
<?=$html->css("schedule.css?t={$t}") ?>
Thus, the link to the css file contains an additional tag at the end, but it remains the same until the file has been modified. This means that the browser will be able to cache it as usual. However, as soon as the file is modified, the link will change and the browser will not skip a bit.
However, this method remains a little dirty to use for something that is not intended. You can not use the "message" that is sent, in the sense that the content of the message matters, except to determine its existence.
Here are my questions:
- Is it possible to admit that there is a risk that the client will see revised HTML with outdated style sheets (or javascript) from the browser cache?
- Is this a hack or a legitimate decision?
- Is there a better solution?
source
share