Problem:
When I modify the static css or js file under webroot/css or webroot/js and then release a new version of my web-application, browsers do not detect the change, so users see a broken page (because of stale css or js).
Solution
I implement a custom version of AssetManager and put it in the components/AssetManager.php:
<?php
class AssetManager extends CAssetManager {
protected function hash($path)
{
return sprintf('%x',crc32($path.'-'.Yii::getVersion().'-'.Yii::app()->params['version']));
}
}
?>
Then I add the following in the main configuration file:
// application components
'components' => array(
'assetManager' => array(
'class' => 'AssetManager',
),
),
// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params' => array(
// this is used in contact page
'version' => '1.0',
),
Now when I release a new version of the App I can change the Yii::app()->params[‘version’] and all of the assets will be republished under different paths.
Question
May be there is a better way to solve this issue? How do you solve it?