Forcing the browser to reload css when it has been updated

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?
+3
source share
2 answers

I don’t think there are any problems with your approach. This is most commonly referred to as cache defragmentation or cache invalidation.

, URL-, : http://site.com/assets/mystyle.css?29320202020, CDN. CDN GET , , , CDN.

, busting cache : http://site.com/assets/mystyle.2390202202.css. Assetic library, , , , mystyle.css mystyle.2390202202.css Assetic " " .

CDN.


:

  • ETags: ETag ( ), , , : HTTP- .

  • expires: , , , . : , .

busting : expires 5 . http- , busting cache.

+4

CakePHP . :

//Configure::write('Asset.timestamp', true);

core.php

(js, css, ). querystring, . .

, . - "?" URL-, , , , , phpdev.

+4

All Articles