when to publish assets

When/where should I publish assets?

I have a library of components and templates that I reuse across projects, so I’ve collected them all in a folder under “protected” - if I need to publish some JS or CSS files from this folder to a public folder, I could use CAssetManager, right?

But where/when should I do the call to the publish method?

Where/when does Yii publish it’s own assets?

And how does publishing assets affect runtime performance? Is there some way to ensure that publishing (or checks required to see if files are published) will be run only once on production servers? Presumably checking for modified assets on every request would be disasterous… (?)

Assets publishing is meant to make components self-contained. It does lowers performance because it needs to check if file/directory copy is needed for EVERY request (via filemtime() calls).

For small to medium sized sites, the performance impact should be minimal because each of your pages may only require a few asset files.

For large sites, it is better not to use asset publishing.

If you do not want to reuse your components (or you know how to take the trouble to manually copy relevant js/css/img files), you don’t need to use assets.

Publishing a whole directory is much faster than publishing individually each file under the directory.

I like the concept of assets, especially since my base of reusable stuff is growing.

And the framework itself publishes all of it’s assets, so not using this feature would be sort of tricky.

But I wonder if there would be some clever way to prevent publishing more than once on production sites?

If you change an asset file, Yii would have to know which one or at least know that something has changed. So you could modify asset manager to create a special file (like assets.lock). If you now change an asset file you delete assets.lock, then the asset manager will re-publish assets if needed and re-create assets.lock. So you will reduce all those filemtime calls to only 1. But I don’t really like that.

Another way would be a cache-locking. So after publishing, the asset manager will cache the state for say 10 seconds, after that re-publish (if something has changed). So on a large site with many unique users you could save like hundreds of filemtime calls with the drawback that a user may get an old asset file (when something changed and caching is still active).

Best solution regarding up-to-date data with no file-access would be something like FAM as a looping console command. See here for example. But I guess it’s too complex for the small benefit and it doesn’t work on windows.

For now, I benchmarked the extra call to CAssetManager::publish() - even under worst case conditions (PHP on a Windows laptop with no bytecode cache, Yii cache disabled, YII_DEBUG enabled) the publish takes < 1.5 milliseconds.

Considering the development/deployment hours this feature will save, I’m not concerned with this tiny overhead :slight_smile: