Combining and Minimizing js/css Files

I have read the section in Yii guide about Combining and Minimizing js/css Files,and my question is,since every page request uses a different set of css/js assets,am I wrong to think that someone needs file merging and script mapping for EVERY page in an application?This sounds quite a laborous to me,specially If someone uses so many widgets and extensions,how can he keep track of all of them,in every page?I will appreciate any enlightening comment on this.

There is minify, a library that make merging/compression.

Take a look at this extension, I never used but looks good.

[color=#1C2837][size=2]Thanks Zaccaria,I already tried a similar extension which minfies and combines the js/css files.It does that automatically: for every page request it generates a different combined js and css file.Unfortunately the js combined file messes with the order of the scripts so it messes up with my slider.My question is:If for some reason someone decided to do this css/js optimization manually-without the use of an extension,just like the Perfomance -Tuning section in in Yii guide suggests,whould it really be possible to do this for every page request,since every page uses different js css assets,so someone whould have to keep track of all the different js/css files in order to combine them to an all.js file and do the mapping in scriptMap of clientscript (for every page) . [/size][/color]

I haven’t tried this solutions but my opinion is that by doing many “all.js” files you don’t really get a better performance…


let’s say we have 3 pages… all three use different set of JS files… but some are common…

So if we have 3 all.js files… one per page…

A user gets to the first page… his browser dowloads the all.js file… gets to the second page… the browser has again to download the all.js file for this page and the same for the 3rd page

So one solution would be like you wrote to combine all JS scripts used in the webapp in one giagntic JS file… but this way it can happen that a user gets to only few pages of our site… and the browser has to download this "can be really big" JS file…

Other solution would be to compress the common or most used JS files… and include the other…

On the other side… recently began to energe a new concept… javascript loaders… I don’t have a reference right now you will need to search the internet a bit… but I read about some loaders that can load JS files asynchonously keeping track of dependencies among them…

If you want to do manually, you can extend the CClientScript .

Create a new class named MYClientScript extends CClientScript, and here you can override the render methods, in order to make your customization.

In this case you can continue registering all script as usual.

Thank you Maurizio for your answer.After reading the relevant section on Yii guide,I wrongly assumed that we are supposed to optimize all js/css files in our application.I now realize that even if this is possible ,it’s too much work and not guaranteed to produce the expected results.So I guess I will follow your advice on optimizing only the most common/used js files.


Thanks Zaccaria.Although a custom Class would offer full control on what js/css is published,this is going too far for me,in terms of time that I can spend on it,at least right now.

Thanks for taking the time to help.