Will creates nice web-things, builds awesome ideas, and collaborates with other creative types.

Home > Blog > Taking my CakePHP Site from a Yslow F to an A

Taking my CakePHP Site from a Yslow F to an A

by Will on Oct 27th 2010, 08:35

This morning, I had an unexpected couple of hours to kill since the surf was flat and my client project got killed. I decided to polish up the site with a bit of speed optimisation.  My YSlow score was an F, for fail.

1st up, I knew i needed to get mark storys asset compress up and running.  I had tried before, but to no avail:  (thats me, floundering at the bottom.)

So, download and unzip the asset_compress plugin to /app/plugins

Open the ini file supplied and add in filters to minimise javascript and CSS
 

Show Plain Text
  1. filters[] = JsMin
  2.  
  3.     and
  4.  
  5. filters[] = CssMin

And download the compression classes here  and here.
Looking in the plugins source code, you can see where to put the classes.  /app/vendors/CssMin/CssMin.php  and /app/vendors/JSMin/JSMin.php

Now add the routes into your routes.php file:

Show Plain Text
  1. Router::connect('/cache_css/*', array('plugin' => 'asset_compress', 'controller' => 'css_files', 'action' => 'get'));
  2.  
  3. Router::connect('/cache_js/*', array('plugin' => 'asset_compress', 'controller' => 'js_files', 'action' => 'get'));

To add the files  in your layout file (probably default.ctp),  replace the trusty $html->css and $html->js calls with:
 

Show Plain Text
  1. $assetCompress->script('jquery');
  2.  
  3. $assetCompress->script('jquery.ui.min');

etc for the css and then for js:  

Show Plain Text
  1. $assetCompress->css('reset');
  2.  
  3. $assetCompress->css('960');
  4.  
  5. $assetCompress->css('text');
  6.  
  7. $assetCompress->css('site');
  8.  
  9. $assetCompress->css('styles');
  10.  
  11. $assetCompress->css('tractor');

(you can see why i knew sticking them together and squishing them was a good idea!)
Now to output the compressed files:  

Show Plain Text
  1. echo $assetCompress->includeCss();    
  2. echo $scripts_for_layout;
  3. And <a href="http://developer.yahoo.com/performance/rules.html)" target="_blank">add the js at the bottom</a>
Show Plain Text
  1. echo $assetCompress->includeJs();
  2. Now that the Javascript files are included at the bottom of the page, dependent scripts entered into the view files won't work.  A good example being the jquery watermark code i had chucked into my signup element.


A quick squizz at the cookbook shows how to  put your scripts in the buffer:  

Show Plain Text
  1. $a  = "jQuery(function($){
  2.  
  3.        $(\"#UserUsername\").watermark(\"Your Name\");
  4.  
  5.        $(\"#UserEmail\").watermark(\"Your Email\");
  6.  
  7.     });";
  8.  
  9.    
  10.  
  11.     $this->Js->buffer($a);

and now  put this:

Show Plain Text
  1. echo $this->Js->writeBuffer();

at the bottom of the layout ctp, below includeJs so that likely dependencies are dealt with.  
  Ok.  I'm done.  Its made a massive difference....I'm on a C.  C?! I want an A!!!
Over to my .htaccess file for some further tweaking: Following the YSlow instructions for each F i was getting;  

Show Plain Text
  1. #disable ETags
  2.  
  3.  
  4. FileETag none
  5.  
  6.    
  7.  
  8. #Some headers and caching stuff to help improve user QoE
  9.  
  10. <IfModule mod_expires.c>
  11. ExpiresActive On
  12.  
  13. ExpiresDefault "access plus 5 minutes"
  14.  
  15. ExpiresByType image/gif A2764800
  16.  
  17. ExpiresByType image/png A2764800
  18.  
  19. ExpiresByType image/jpg A2764800
  20.  
  21. ExpiresByType image/jpeg A2764800
  22.  
  23. ExpiresByType image/x-icon A2764800
  24.  
  25. ExpiresByType text/css A2764800
  26.  
  27. ExpiresByType text/javascript A2764800
  28.  
  29. ExpiresByType application/js A2764800
  30.  
  31. ExpiresByType application/javascript A2764800
  32.  
  33. ExpiresByType application/x-javascript A2764800
  34.  
  35. ExpiresByType application/x-shockwave-flash A2764800
  36.  
  37. </IfModule>
  38.  
  39.    
  40.  
  41. #Gzip compress some content types
  42.  
  43. <IfModule mod_deflate.c>
  44.  
  45. AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/x-javascript
  46.  
  47. </IfModule>

And now I'm on...B. B for boo.  what else can I do?  
Oh, turn off debugging to exclude the toolbar.  hay presto! A.  And a 100 score from google page speed. Win!

 

Bonus:, to add compress to your admin area, target the build file like this:

Show Plain Text
  1. $assetCompress->css('reset', 'admin');
  2. echo $assetCompress->includeCss('admin');

Special thanks to Mark, awesome awesome work.  

Taking my CakePHP Site from a Yslow F to an A cakephp

blog comments powered by Disqus