[EXTENSION] ExtendedClientScript - Reduce your page loading times

Still no luck? Is the error on the same line? Could you post the line? I use Yii SVN: http://yii.googlecod…vn/branches/1.0

Quote

Do you have the google code SVN?

Sorry, I was confused that you had attached your code to the article.

Quote

Still no luck? Is the error on the same line? Could you post the line? I use Yii SVN: http://yii.googlecod...vn/branches/1.0

Yeah, there is no error this time and my application works fine. After I compare two HTML before/after enabling this extension, only one line differs. Is it expected?

Quote

  // -->

  </script>

 

! <link rel="stylesheet" type="text/css" href="/demos/yii-blogdemo-enhanced-extension/assets/4b0d6480/calendar.css" />

  <title>blog - Post</title>

 

  </head>

— 22,28 ----

  // -->

  </script>

 

! <link rel="stylesheet" type="text/css" href="/demos/yii-blogdemo-enhanced-extension/assets/8e71a30/c_a9db2e66ee865f3702bb4da440f176f9.css" />

  <title>blog - Post</title>

 

I am not sure what the condition of this extension is. Does this work with the code like this?

protected/views/layouts/main.php:

<script type="text/javascript" src="/demos/yii-blogdemo-enhanced-extension/js/jquery-1.3.2.min.js"></script>


<script type="text/javascript" src="/demos/yii-blogdemo-enhanced-extension/js/highslide/highslide.js"></script>


The ExtendedClientScript works with CClientScript. So it will combine and compress files registered with the CClientScript routines.

For example, in your controller (or view/layout) you have:



		$cs=Yii::app()->clientScript;


		$cs->registerCoreScript('autocomplete');





		$cs->registerScriptFile('/assets/js/plugins/tooltip/jquery.tooltip.js', CClientScript::POS_HEAD);


		$cs->registerScriptFile('/assets/js/ui/ui.core.js', CClientScript::POS_HEAD);


		$cs->registerScriptFile('/assets/js/ui/ui.tabs.js', CClientScript::POS_HEAD);





Normally the above will add about 6 Javascript tags to your head section after calling render().

Without changing the above code, using ExtendedClientScript will automatically combine these files to one, and compress them with JSMin. After that you will have only one Javascript tag rendered in your head section.

This saves 5 HTTP roundtrips from client to server, and reduces the amount of data transferred to the client. This will give your pages a more responsive feel, and saves you a lot of data transfer on top.

Same goes for your CSS files registered with registerCssFile() (or registerCoreScript()). All separate CSS files will be combined, and CSSTidy will compress and correct your CSS.

For CSS files with relative url's (like jQuery UI themes) you need to convert the url's to be a relative to root url. I might add an additional routine to automatically convert the relative url's in a later version.

Thanks!

There seemed to be no description there in terms of registerScriptFile, because it is too natural.

Thank you again for a nice extension. I have changed my application that can be acquired from the google code using your ExtendedClientScirpt extension. Karma++ ;)

BTW, when I tried to generate a module using a shell command such as '…/…/framework/yiic shell' under the application directory, I got an error as below.

Quote

<h1>PHP Error</h1>

<h3>Description</h3>

<p class="message">

filemtime(): stat failed for /assets/8e71a30/jquery.js</p>

<h3>Source File</h3>

<p>

/var/www/html/yii/demos/yii-blogdemo-enhanced-modules/protected/components/ExtendedClientScript.php(189)</p>

Actually when I turned ExtendedClientScript configuration OFF in the config/main.php, there is no error. Does this error come from the ExtendedClientScript?

Hmm yes, I have the same issue. This is caused by using $_SERVER['DOCUMENT_ROOT'] if the filePath and basePath are not set in the config.

In a command line environment they are not available unfortunately. Currently I have no other solution than defining basePath and filePath in config.

I'm open for other suggestions.

I just tried following code but got no success :cry:

dirname(Yii::app()->getRequest()->getScriptFile())

I think ExtendedClientScript is for a performance purpose, how about to disable it when $_SERVER['DOCUMENT_ROOT'] is undefined?

No, you probably can't use that. Have a couple of options for you:

In config/main.php:



if($_SERVER['DOCUMENT_ROOT']=='') define('CLI', true);


...


return array(


...


	'components'=>array(





	    'clientScript'=>array(


			'class'=>'application.components.ExtendedClientScript',


			'combineFiles'=>defined('CLI')?false:true,


			'compressCss'=>defined('CLI')?false:true,


			'compressJs'=>defined('CLI')?false:true,


		),


...


Second option in config/console.php:



$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..';


Think the second is the nicest since it's only for cli, so no performance hit at all.

Found that the Yii class CFileHelper suffers too if you use that in your application, when using cli.

First off, thanks Maxximus for the great extension.

I made some slight modifications to support themes a little, with a sort of cascading file system.  I'm not sure that this is the right place to do this, or even if its the "correct" way of doing it, as I have only been using Yii for a few days.  I just figured I would share my code in the hopes that someone else finds it useful, or could point out its flaws.

Change in combineAndCompress:

Total combinAndCompress function:

What this does is allow you do use something like:

and it will use /assets/css/admin/main.css by default, but if a theme is enabled, and the file /themes/THEMENAME/assets/css/admin/main.css exists it will used the theme version instead.

Hopefully this is useful to someone.

Thanks! I didn't use themes yet myself, but are these urls not already replaced by the theme manager?

If not, how does Yii handle this normally, since the extension grabs the urls at the latest possible moment.

Hi Maxximus,

I'm trying to use your extension to see how does it work with the jui extension, but I'm getting this error:

but:

  • [tt]jquery.js[/tt] is a core script.

  • My app is in my public_html (user's web directory), not in /var/www (why is this being appended?)

I'm using the latest yii from the svn repository. My [tt]main.php[/tt] config is the example given in the extension.

Thanks in advanced.

Hi MetaYii,

First of all, you can (will) run into some issues with JUI, especially when used with ThemeRoller themes, because of the relative paths used there.

For your current issue: It seems something goes wrong with the determination of the following: realpath($_SERVER['DOCUMENT_ROOT']). For one reason or another your $_SERVER['DOCUMENT_ROOT'] is still the default /var/www/.

Besides that, I doubt that the use of relative filepaths (~metayii/) will work as expected. Could you please try to set basePath to the correct absolute path (/home/metayii/Desarrollo/YII/yii-svn/demos/helloworld2/) in main.php, and see if things work as expected?

Hi Maximmus,

I have tried out this extension as well.

I think it might be advantageous to be able to choose to compress CSS or JS files separately, so you don't run into issues with relative paths in CSS but you still want to compile and compress your published JS scripts into one file.

Hi, sorry for my late reply. You can already set almost everything in your main.php config, for instance:



	    'clientScript'=>array(


			'class'=>'application.components.ExtendedClientScript',


			'combineFiles'=>false,


			'compressCss'=>false,


			'compressJs'=>true,


		),


Only thing is that CSS and JS will be combined with combineFiles. In another version I will separate that.

Maxximus: as far as I can see, the files to compress/combine are get by their URIs, right?

No, the files are got by their absolute filepath to compress and combine. After that the Yii function remapScripts() points them all to the created file, and Yii will only output the correct HTML line once.

In general, I only touch some variables used by Yii::clientScript just before output time. But because of location change (writing the generated file to the temporary Yii cache directory) relative URL's won't work anymore.

I could adjust the relative URL's in the generated file pretty easy by prepending the relative URL's with the original URI, but other options are appreciated.

Maxximus, first of all, Great extension. I like it.

Secondly, according to my googling result, you do not need to use the DIRECTORY_SEPERATOR constant. I find it causes problem in my local Windows machine. Check the following link,

http://old.alanhogan…r-not-necessary

Cheers,

Well, I actually used DIRECTORY_SEPARATOR to enhance compatibility ;). Yii itself uses it a lot too, so I’m surprised it is only giving trouble here.

Will remove it in a next version.

Quote

Secondly, according to my googling result, you do not need to use the DIRECTORY_SEPERATOR constant. I find it causes problem in my local Windows machine. Check the following link,

http://old.alanhogan…r-not-necessary

I checked my program and found that I have used DIRECTORY_SEPERATOR in my URL. Thank you.

http://www.yiiframew…opic,835.0.html