Jump to content

[Solved] CSS Auto Versioning to eliminate browser caching


Recommended Posts

I am running into a lot of browser cache issues with the global.css from customers who are not technical enough to use the F5 key.

http://particletree.com/notebook/automatically-version-your-css-and-javascript-files/

I am wondering if someone with solid PHP skill can assist in implementing the above suggestions into /class/tools.php so the addCSS function can include the auto versioning of CSS files to prevent future browser caching.

I think this is a nice feature to include in future releases, otherwise a CSS browser cache problem makes PrestaShop look broken when it is not.

PrestaShop version 1.4.0.17

Link to comment
Share on other sites

    /* rewrites url with timestamp */    
   public function autoVer($url){
   $path = pathinfo($url);
   $ver = '.'.filemtime($_SERVER['DOCUMENT_ROOT'].$url).'.';
   $verCSS = str_replace('.', $ver, $path['basename']);
   return $verCSS;



I added this to the classes\Tools.php and was able to pass $verCSS value back to addCSS in the FrontController.php file.

The FrontController.php file is updated to this starting at line 397:

   public function setMedia()
   {
       global $cookie;
//        Tools::addCSS(_THEME_CSS_DIR_.'global.css', 'all');

       $verCSS = Tools::autoVer(_THEME_CSS_DIR_.'global.css');

       Tools::addCSS(_THEME_CSS_DIR_ . $verCSS, 'all');



After this, the HTML source code is looking for the CSS file with versioning.

<link href="/xxxxx/themes/xxxxx/css/global.1306108222.css" rel="stylesheet" type="text/css" media="all" />



Anyone like to chip in?

Link to comment
Share on other sites

The suggested .htaccess rewrite rule is this

#Rules for Versioned Static Files
RewriteRule ^(scripts|css)/(.+)\.(.+)\.(js|css)$ $1/$2.$4 [L]



Anyone know how to get this to work for the correct path such as

/themes/xxxxx/css/global.1194900443.css

Thanks.

Link to comment
Share on other sites

  • 4 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...