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?
问题:
回答1:
there is additional property as
if (YII_ENV_DEV) {
...;
...;
...;
$config['components']['assetManager']['forceCopy'] = true;
...;
...;
}
to publish files even there are published before
回答2:
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
回答3:
If you are developing your own plugin, you can force publish assets per bundle (note: $sourcePath should be set)
<?php
namespace app\components\forms\redactorAssets;
use yii\web\AssetBundle;
class RedactorCutAsset extends AssetBundle {
public $sourcePath = '@app/components/forms/redactorAssets/assets';
public $js = [
'cut.js',
];
public $publishOptions = [
'forceCopy'=>true,
];
}
回答4:
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.
回答5:
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
回答6:
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.
回答7:
I configure the assetManager::forceCopy=true
in main-local.php for the dev environment like this
return [
'components' => [
...
'assetManager' => [
'forceCopy' => true,
]
...
],
];
回答8:
sudo rm -rf frontend/web/assets/*
sudo chmod 777 frontend/web/assets
./yii cache/flush-all
If this does not work:
sudo rm -rf vendor/*
composer install