About assets folder and feature

Hi there,

Can anyone point me to a place where I can find more info about how assets feature works? Or provide me with such information in general? If I’m not mistaken this topic is covered in Yii documentation in short and with not enough details.

I would need some basic info:

[list=1]

[*]How to control when and which files yii puts there?

[*]Can I delete these files when moving application from developement to production server?

[*]Can I delete contents of this folder from time to time, if it gets to big in size?

[*]What is the difference between assets and protected/runtime folders? If I understand yii documentation properly both are being used for the same reason - to store files being used temporairly, only in runtime.

[/list]

And so on.

The problem that troubles me much is that yii is getting rather space-hunger when using this folder / feature. For example I found out in very basic application build on base of yii demo app that I have three copies of jQuery put into three different folders, each occupying 745 kB. And THE only moment jQuery was used in my app was a ONE TIME run of gii for generate one example model. If I’m not mistaken I also noticed some demo apps unpacking jQuery to assets folder where jQuery was not used at all - i.e gii was disabled as by default and absolutly no jQuery related code was used in the page.

Three copies of jQuery that is 2,18 MB of additional webspace which is being wasted in my opinion. If we go this way and app can get really really big after being used for months and for many users! :( I’m generally not get used to app extracting any extra files into some folders where I have rather no control on which files, where and when are put. I’m an old-school web-developer where knowing exactly what file is where and for what reason is the base for maitaining webapp good health.

Can’t understnad why it isn’t solved the way with fixed folder names - i.e. jQuery is being extracted to a fixed folder name under assets and yii core only checks if that folder name exits, if not - recreate it. What is the reason to have three or more exactly the same (byte-by-byte check) copies of jQuery under assets folder? Especially if you take under consideration that if jQuery gets updated while yii not, one still will have to manually ‘override’ this feature and add new version of jQuery on it’s own.

Thanks in advance for any explanations in this topic!

Yii puts new files or folders to the assets folder each time some class calls Yii::app()->clientScript->registerCssFile($cssFile) or Yii::app()->clientScript->registerScriptFile($jsFile).

This feature are used by almost all widgets, see guide for details.

Also try to search forum for "assets" - there are many topics with questions similar to yours.

In short:

  1. Assets main purpose is to allow to keep js/css files in the protected folder near widgets which uses these css/js files and then copy them to public assets folder via Yii::app()->clientScript.

  2. Difference between assets and runtime is that assets is in the public accessible folder, while runtime is protected.

  3. You can safely remove all content from the assets folder (necessary content will be regenerated during next request).

  4. Sometimes you should clean it, because if some source folder content is changed (for example, you updated some widget js/css) then yii will not republish folder automatically.

It is important to note that after a framework upgrade, Version 1.1x to 1.1xx the naming of the individual assets folder will be different because Yii uses the version number when creating the hash.

So when you upgrade your framework version you should clear the assets directory.

doodle

I would like to reopen the discussion.

If Yii calculates assets basing on hash, then how to explain that after using an app for a month I have three exact copies of jQuery UI in assets folder of which two were created in the same time (see folder dates)? See attachment:

951

assets_problem.png

Can someone explain me what is the reason in such non-sense space waste? Isn’t that a bug?

I believe this is the classic trade off between space and size versus performance.

Waste of space? In this day of age?

Did you check the size of the directories? ;)

Without knowing exactly what goes on in Yii, I think it has to do with modules/extensions/widgets publishing their own set of assets.

I don’t know for sure.

But as long as it’s only three directories with a total size of max 3 MB, then worry not.

I found something for you, Trejder:

How to prevent jquery.js and other core scripts from being automatically published and inserted in your pages

HTH :)

No, it is not a matter of size or space, of course. In these days - as you say - when you can have 750 GB hosting for as much as ten bucks / month. But I’m trying to figure out - with a little help of more experienced Yii developers - if the fact that I have two different copies of jQuery generated exactly in the same moment (the same date and time up to particular minute) is trully a feature of Yii or bug?

Here goes about whole picture. Three directories and 3 MB for a week or two of running test-drive mode by one user with as much as ten request pers minute. If we go this way, then what about a year or half of running in production mode, being used by thousands of people and generating millions of requests per second?

Thanks for the link below. But still - I’m not trying to find out how to stop this behaviour but to make myself sure that it isn’t truly a bug (as it look for me right now) but fully controlled and code-optimised feature - i.e. it have to go this way, even if it looks like a bug.

You’re right.

And I guess it’s due to widgets publishing their own stuff, and maybe not behaving as they should.

I think I read a discussion about this recently: about sharing resources/assets across widgets.

If you remove all your widgets, then you probably would end up with one asset directory?

It’s puzzling to me too, as a matter of fact. ;)

No, you certainly not! This is what I’m talking about and this is what worries me the most!

Right now I’m developing only a small application with ONE additional widget - menu (rest of assets folder contents comes as garbage after different menu system tested and are not recreated after remove) - and with extensive use of jQuery.

Even if I use only jQuery and one widget I have three exact copies of jQuery after one month of using this app alone. What about, if it will be used months by thousands? What about if I introduce more widgets?

Did you maybe update your Yii version? Check the CAssetManager::hash() method. This is where the hashed directory name for a file to publish is generated. It only depends on the source file (or directory) name + the Yii version. If none of both changes, there will be no new folders in assets.

Mike,

I WAS informed (earlier in this topic) that assets hash depends only on file or directory name and Yii version. Therefore there must be a bug in yii about core (‘jquery’) lib itself. Because I didn’t neither upgraded Yii (still on 1.1.4) nor I changed folder name, as we are talking about jQuery, which is a part of Yii and therefore user has nothing to do with its source folder name.

Yes, I can confirm, observations that I have (about doubling, tripling or more of assets folder) are only related to core JS scripts that ships with Yii. I haven’t observed generating more then one assets folder for scripts and other assets that are user-related.

Seems to be bug, though reported it.

Sorry, didn’t read the complete topic again.

Well, maybe it’s best if you do some debugging for yourself if you’re not afraid of reading some source code. In my opinion it never hurts to get a better understanding of the internals of Yii.

The interesting locations are (in the order they are usually executed):

http://www.yiiframework.com/doc/api/1.1/CClientScript#registerCoreScript-detail

http://www.yiiframework.com/doc/api/1.1/CClientScript#getCoreScriptUrl-detail

http://www.yiiframework.com/doc/api/1.1/CAssetManager#publish-detail

http://www.yiiframework.com/doc/api/1.1/CAssetManager#hash-detail

Trejder, did you tried to clean up assets folder (remove all folders) and then check whether you still have tree copy of jquery in assets (you need to make some request to get new assets published)?

Are all three folders have exactly the same content?

Hi seb,

I’ve been using app I’m talking about for around 1 month and since that time I cleaned assets folder at least five times. I did not looked to assets after each request, but I noticed that jQuery asset is being republished after each assets folder clean, once in two copies once in three.

I don’t have time to check each file, but taking in account that each of this copy has exactly the same size (745 145 bytes) and the same file/folder structure, then I assume that yes - these folders has exactly the same content.

Today I discovered another, as I think weird, thing. If assets folder name is being generated only basing on folder name (internal, not changing as we are talking about jQuery that ships with Yii) and only on Yii version then how it is possible that name of that assets folder has changed, if Yii hasn’t been upgraded by me? Once it was 9d8be0eb, another time 67…something, now it is 418d6bd7. Size still exactly the same - 745 145 bytes. Isn’t that something weird?

For me I can get duplicate assets only when I use the same sources folder from windows and linux (I have andLinux which runs inside windows).

But this is because real paths are different in windows and linux because of different directory separator (’\’ vs ‘/’).

What can cause publishing assets twice:

  1. Yii version changed

  2. Real path of the source folder changed

  3. You have two copies of the same assets in you sources tree and they published from different locations.

  4. Somewhere publish method is invoked with second parameter ($hashByName) true.

As I see there is no more possibilities.

For me if I clean assets and make a request then the same folders with the same content is regenerated.

So if you really worry about issue you have with assets then try to make some experiments (or some debugging) to find out what is going on.

Mike,

Sorry I missed your post, while it was posted.

No, I’m not afraid of reading source codes! :) Actually, reading Yii codes is a really interesting adventure as they are written (at least in my opinion) in so clear and professional that one can learn a lot out of it.

I did some researches in source code after Qiang’s replies to the bug I reported and found possible answer to my problem. See below post.

Seb,

After doing some dig in Yii source code I think I found out solution. It seems that I misunderstood the idea of what path means here. For all the time I assumed that hash is being generated upon relative path to internal jQuery asset up upon Yii main dir. But it turned out that it must be an absolute path. My Yii version hasn’t been updated (though contents of Yii folder hasn’t changed in anyway) but I recently updated my server package from XAMPP Lite to XAMPP full and therefore it’s (and Yii’s) absolute path has changed from x:\xampplite to x:\xampp (under Windows).

It happened on 2010-11-11 11:42 and this probably caused regeneration of assets folder.

It still concerns be (if you take a look at screenshot I provided) why there has been two exact copies of jQuery asset generated exactly on the same date, hour and minute on 2010-11-10 15:04 as I don’t recall any changes to Yii or folder structure by that date. But this will be hard to explain and I believe it will be a good idea to end this discussion (again).

As I wrote in bug report, if I find something new in this topic I’ll write here or in bug report, but looking on the Yii source code it seems that no matter what I’ll find I will be 99,99% cause of my changes made in for example folder structure. It seems that this is truly not a bug, so I was wrong assuming this.