Yii2 assets clear cache

2019-01-18 01:41发布

Everytime I update my css or js files in infoweb\menu\module\assets, I have to empty the backend\web\assets folder
is there a way to automatically clear the assets cache?

标签: php assets yii2
8条回答
▲ chillily
2楼-- · 2019-01-18 02:17

I use CClientScript::registerScriptFile method in my view files:

Yii::app()->clientScript->registerScriptFile(
    $this->getAssetsBase() . '/js/script.js'
);

If I modified script.js, after next page reload I will see all changes

For css files - CClientScript::registerCssFile

Yii::app()->clientScript->registerCssFile(
    $this->getAssetsBase() . '/css/style.css'
);

UPDATE: if you use yii 2.0 beta, you can read some information about changes in mechanic of client helpers here: link

查看更多
倾城 Initia
3楼-- · 2019-01-18 02:19

I configure the assetManager::forceCopy=true in main-local.php for the dev environment like this

return [
'components' => [
    ...
    'assetManager' => [
        'forceCopy' => true,
    ]
    ...
],
];
查看更多
放我归山
4楼-- · 2019-01-18 02:20

The AssetManager will create a hash based on the file modification time. The modification time of a directory does not change when any file is changed. If you have an AssetBundle that has a directory as $sourcePath, the modification time of the directory is used, the hash will not change, and nothing gets copied to the web/assets directory.

I suggest overriding AssetManager::hash() in a subclass or write a function for AssetManager::$hashCallback:

'components' => [
    'assetManager' => [
        'hashCallback' => function($path) { 
             // if: $path is directory: figure out when files were changed in directory 
             // else: use original hash function in \yii\web\AssetManager
        }
    ], 
]

For a sample implementation for finding the max modified date over all asset files in a bundle you could look at this comment by wookie @ http://php.net/manual/en/function.filemtime.php#35779

Note that modification to any asset file will create a new directory in web/assets, and regular cleanup will remain necessary. However, browser cache aside, refreshing the page will follow the latest changes.

查看更多
5楼-- · 2019-01-18 02:21

there is additional property as

if (YII_ENV_DEV) {
       ...;
       ...;
       ...;
       $config['components']['assetManager']['forceCopy'] = true;
       ...;
       ...;
 }

to publish files even there are published before

查看更多
不美不萌又怎样
6楼-- · 2019-01-18 02:21

Add this in your view:

use vendor\myVendorName\myPackageName\assets\AppAsset;
AppAsset::register($this);

Add this in your config:

'components' => [
    'assetManager' => [
        'linkAssets' => true,
    ], 
]  

Empty assets folder, and refresh, done

查看更多
Evening l夕情丶
7楼-- · 2019-01-18 02:26

If you enviroment is production I recommend to use Cache Busting :

return [
 // ...
    'components' => [
        'assetManager' => [
            'appendTimestamp' => true,
        ],
    ],
];

for more information about assets, read the Assets Yii2 documentation.

查看更多
登录 后发表回答