Ruben
Ruben

Reputation: 9186

Is there a way to force Yii2 to reload module assets on every request?

My website is divided into separate modules. Every module has it's own specific css or js files.
Yii's assetManager creates a folder when I first open a page that uses my assets.

Unfortunately if I change something in my files Yii 1.x does not reload my css or js files.

I have to manually delete the web/assets folder. It is really annoying when you are developing the app.

This works when I add a module to the backend folder, but not when I'm creating a module in the vendor folder with my own namespace.

Upvotes: 3

Views: 10188

Answers (3)

SasHok
SasHok

Reputation: 21

You can set forceCopy = true.

class Assets extends AssetBundle{

    public function init()
    {
        parent::init();
        $this->publishOptions['forceCopy'] = true;
    }
}

Upvotes: 2

Pedro
Pedro

Reputation: 898

In Yii2 you can append a timestamp to the URLs of assets like this...

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

This won't force the assets to reload on every request but whenever an asset file is changed the URL will change because of the timestamp & that will force the asset to be re-published.

Upvotes: 2

Manquer
Manquer

Reputation: 7627

With respect to Yii1.x With assetManager you can do this by setting 'forceCopy' attribute to true in your config file

... copy the asset files and directories even if they already published before. This property is used only during development stage

See forceCopy documentation here for more info.

Alternatively you can use linkAssets which will not copy the files but create an soft link between your asset files and yours assets directory. You cannot of course use both.

For the second part of the question I am assuming this is in Yii 2.x, you are supposed to use AssetBundles, you can register any namespace bundle from anywhere, you simply register it in the view with some like this

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

Upvotes: 0

Related Questions