View RSS Feed

MPC

Tips to Speed up your Website

Rate this Entry
by
MPC
on 03-09-2010 at 10:31 AM (2516 Views)
For the last week or so, I’ve been reading up on ways to make my webpages load faster and did a lot of testing. I'll save you the hassle of trying to figure this stuff out, and just tell you what to do (Or at least, what seemed to work well for me).

Please note that the performance tweaks in section 5 below require an .htaccess file so if you’re on a windows server, this will not work for you. I know there's other ways to make those tweaks without using .htaccess, but you'll need to research this on your own.

1. ****BACKUP YOUR FILES*** Just in case! If something doesn't go as planned and you're confused as to how to fix something, having a backup to restore is a good idea. No need to back up a database is you use a CMS as this won't affect it. But do backup your CSS files, JS files, and template files.

2. Start by downloading some tools. You’ll need:
• The latest firefox browser: Firefox Browser | Faster & Safer Internet | Free Download
• firebug add on: Firebug
• Google’s Speed Page add on: Install Page Speed
• Yahoo’s Yslow add on: https://addons.mozilla.org/en-US/firefox/addon/5369

3. Visit your site’s hompage with firefox. Hit F12 to turn on the firebug. Go to the pagespeed tab and click “analyze performance”. You’ll get a score plus a bunch of information things you can improve. Have a quick look at these, then repeat the process with Yslow. Both of these tools give you similar information. Now you know how to test your stuff. For the purpose of this blog post, I’m just going to stick with “Speed Page”.

4. Simple Speed Page (SP) elements you can improve – These are common errors that are easily corrected. This applies to windows servers as well as there’s no need to use .htaccess for these performance tweaks:
  • Specify image dimensions – This is the easiest thing to fix. SP will list the images where this information is not specifed, and will also list the actual image size. All you need to do is edit your html code and include height and width parameters in your image tags. As an example, you would go from something like
    PHP Code:
    <img src="domain.com/images/file.gif" /> to <img src="domain.com/images/file.gif" width="120" height="60" /> 
    Telling the browser image sizes saves time because it avoids the unecessary requirement for the browser to make the calculation itself.
  • Optimize images – This simply means to compress your images as much as possible to minimize file size. Again PS does all of this for you. It will provide URLs to the compressed versions. Grab those, upload them to your server to replace the non optimized files. WARNING – PS converts gif to png and jpg to jpeg. You’ll need to edit your HTML and template file (css) to reflect the new file extensions.
  • Parallelize downloads across hostnames – I created a couple of subdomains to send my template images. The idea is that most browsers have a cap of only being able to download 2 files at once per domain. By creating subdomains, it’s possible to transfer more than two images at the same time. So create like 6 subdomains (1.domain.com, 2.domain.com, etc). Create an images folder in each subdomain. Try to divide up your template images equally (in terms of file size) across yoru subdomains. Each subdomain should AT LEAST send 2 images. So let’s pretend that your template uses 20 images and you have 5 subdomains, try to feed 4 images per subdomain. You will need to edit your main CSS file to reflect the correct paths for each images.
  • Combine external CSS – Instead of having the browser request multiple CSS files, put all your CCS into a single file. That way, you deliver the CSS content with a single request. If you use word press, some add ons will have their own CSS. You need to copy all of it into your own main CSS file, then edit the component files to make sure the code doesn’t add a redundant <link rel="stylesheet" …>
  • Minify CSS – Minify simply means make your code as compact as possible. Space characters aren’t required. Comments aren’t required. All of these add to the file size. A good practice is to save a non minified version of your CSS file in case you do need access to comments and to have something readable, but do get your page to load up the minified version. How do you create one? Quite simple! Go to Clean CSS - A Resource for Web Designers - Optmize and Format your CSS and just past in your non minified CSS code, select the options you want, click “process CSS” and the minified code will be generated at the bottom. You can copy this over your current CSS file content (Make a back up of it before, just in case). A cool tool that will combine all the CSS files into one is Google’s Minify minify - Project Hosting on Google Code This app combines all CSS files into one and minifies them, without touching the originals.
  • Combine external JS and Minify JS – This is the exact same principle as what I just explained for CSS. It’s more efficient to call a single JS file than to call 10. You may want to attempt to combine all of those into one. You can minify JS with this simple tool (copy and paste your code it, click “compress javascript”, copy and paste the result back). Minify Javascript Online / Online JavaScript Packer. Again, Google’s Minify Tool can take care of all of this for you. minify - Project Hosting on Google Code

5. Advanced PS elements you can improve (these require using .htaccess)
  • Enabling Compression – There’s more than one way to do this, but I am doing it via .htaccess. First, I was looking into my cpanel and noticed I had a “optimize website” menu in the advanced tools. I turned on “compress all content” which is in fact probably overkill. I could have used the mime type to specify what content to compress. For instance, if some image types are already compressed, turning compression on for those images might actually slow things down. This is something I’ll need to look into further.
  • I then made the following additions to my .htaccess file from the root of my website:
    PHP Code:
    # Learning Apache to understand file type by extention: cascade styles and javascripts.
    # Usually it "*.css" and "*.js" files.
    AddType text/javascript .js
    AddType text
    /css .css

    # Some performance trick for PHP
    <IfModule mod_php5.c>
    #comment out the next line if on wordpress (magic quotes)
    php_value magic_quotes_gpc 0
    php_value register_argc_argv 0
    php_value register_globals 0
    </IfModule>

    # file we pass throughout "mod_deflate"
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text
    /html
    AddOutputFilterByType DEFLATE text
    /xml
    AddOutputFilterByType DEFLATE text
    /css
    AddOutputFilterByType DEFLATE application
    /xml
    AddOutputFilterByType DEFLATE application
    /xhtml+xml
    AddOutputFilterByType DEFLATE application
    /rss+xml
    AddOutputFilterByType DEFLATE application
    /javascript
    AddOutputFilterByType DEFLATE application
    /x-javascript 
  • Gzip compression on your CSS file using php: Create a new file called gzip-css.php in the same folder where you have your main CSS file. gzip-css.php uses the following code:
    PHP Code:
    <?php 
    ob_start 
    ("ob_gzhandler");
    header("Content-type: text/css; charset: UTF-8");
    header("Cache-Control: must-revalidate");
    $offset 60 60 ;
    $ExpStr "Expires: " 
    gmdate("D, d M Y H:i:s",
    time() + $offset) . " GMT";
    header($ExpStr);
    ?>
  • Create a .htaccess file in that same folder with the following:
    PHP Code:
    AddHandler application/x-httpd-php .css
    php_value auto_prepend_file gzip
    -css.php
    php_flag zlib
    .output_compression On 
  • Gzip compression on your JS file using php: Create a new file called gzip-js.php in the same folder where you have your main JS file. gzip-js.php uses the following code:
    PHP Code:
    <?php 
    ob_start 
    ("ob_gzhandler");
    header("Content-type: text/javascript; charset: UTF-8");
    header("Cache-Control: must-revalidate");
    $offset 60 60 ;
    $ExpStr "Expires: " 
    gmdate("D, d M Y H:i:s",
    time() + $offset) . " GMT";
    header($ExpStr);
    ?>
  • Create a .htaccess file in that same folder with the following:
    PHP Code:
    AddHandler application/x-httpd-php .js
    php_value auto_prepend_file gzip
    -js.php
    php_flag zlib
    .output_compression On 
  • Cache expiry on your files – This setting tells the browser to store some files locally so it doesn’t have to reload them every time a new page is requested. This is particularly useful for template related files (images and CSS) as they are static. If you prevent the browser from having to reload all those image files every time visitors browse your pages, you make things faster for them because they will load to files using a copy stored on their hard drive.

    Remember the subdomains we created earlier to store image files? We’re going to create an .htaccess in each image folder of each subdomain. The .htaccess should contain the following code:
    PHP Code:
    ExpiresActive On
    ExpiresByType image
    /gif "A2678400"
    ExpiresByType image/jpeg "A2678400"
    ExpiresByType image/png "A2678400"
    ExpiresByType image/bmp "A2678400" 
    The syntax used is A + number of seconds before cache expires.Currently this is set to a month. If you want a day use 86400 (60 sec x 60 mins x 24 hrs). A week is 604800 (60 sec x 60 mins x 24 hrs x 7 days), and so on. Tempate files that aren’t expected to change should be set to a month or more.

    The .htaccess file located in the folder of your main CSS file should contain
    PHP Code:
    ExpiresActive On
    ExpiresByType text
    /css "A2678400" 
    The .htaccess file located in the folder of your main JS file should contain
    PHP Code:
    ExpiresActive On
    ExpiresByType application
    /x-javascript "A2678400" 

That’s basically what I’ve looked at so far, and a few things I mentioned above are not fully implemented yet on my site. Still, my PS score went from 60/100 to almost 90/100 with these tweaks. I am really not an expert at all with this .htaccess stuff and it's quite possible that I have some code that duplicates things, or doesn’t do anything! If anyone can provide feedback.
If you find this helpfull and want to thank me, throw me a link! xhttp://www.mypokercorner.com on "Poker Strategy". Thanks!

EDIT: One thing I forgot to mention - CSS Sprites. We all use a lot of icons to represent poker rooms. For must of us, each of these icons is a seperate image that must be loaded. This generates an HTTP request for EACH image you want to load, and the more request you send, the slower your page will load. The idea of CSS sprites is that you woudl instead have all these icons as part of one image and you would use CSS the specify which section of the image to display for each icon. In other words, if you had say 20 poker rooms with 20 icons, this method would load up a single image (so 1 HTTP request insetad of 20) and the CSS would "show" a portion of the image for each icon. I find this very interesting. In fact, one could develop an entire site template using this method. More on this, and example codes at http://css-tricks.com/css-sprites/. I'm very interested in this. I'll play around with this in the future.

Submit "Tips to Speed up your Website" to StumbleUpon Submit "Tips to Speed up your Website" to Digg Submit "Tips to Speed up your Website" to Facebook Submit "Tips to Speed up your Website" to Google Submit "Tips to Speed up your Website" to del.icio.us

Updated 03-18-2010 at 02:28 PM by MPC

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments

  1. DealerDan's Avatar
    Don't have much time and just skimmed this, but noticed this and wanted to post:

    Combine external CSS – Instead of having the browser request multiple CSS files, put all your CCS into a single file. That way, you deliver the CSS content with a single request. If you use word press, some add ons will have their own CSS. You need to copy all of it into your own main CSS file, then edit the component files to make sure the code doesn’t add a redundant <link rel="stylesheet" …>

    Be very careful with things like this. It's not as simple as "grab css from one file, put to another then edit the file and good to go". Basically if you don't really know what you are doing, and only doing it because "Firebug says so" then yeah - stop. There's a few other things too I'll get my programmer to come in and explain as he can do it better than me.
  2. MPC's Avatar
    I'm glad you bring this up, because I want to expand on that topic. 2 points to make:

    1 - Combining JS or CSS is not ALWAYS the best way to improve performance. For instance, you might have a single page out of a 300 page site that will have some CSS code that make it very unique. It would be counter productive to load that CSS code on the other 299 pages that won't use it.

    2 - Dealer Dan brings a good point about being careful. That's why I suggested backing up your files before doing this. The thing you have to remember when combining CSS files is to adjust the file paths in consequence. For example, lets say I have the follwoing 2 CSS files:

    domain.com/templates/style1.css

    domain.com/template/custom/style2.css

    If you wanted to combine everything in style1.css, you probably can't just copy the content from style2.css into it. Style2.css might reference images that are in stored in domain.com/template/custom/images and if the path specified is relative, then they won't load if your main css is in a different directory. You either need to adjust all your paths, or move all your images.

    In this example, I could move all images from domain.com/template/custom/images/ to domain.com/template/images/ (assuming there's no duplicate file names).

    But yeah, I guess I should have mentioned that you really need to have some udnerstanding of what you're doing before you get into these tweaks because you could really screw up a site.

    Regarding the CSS generated by WP components, these are usually stored in their own seperate files in the plugin directories somewhere. You CAN combine them, but you need to understand what you are doing. Also understand that future plugin updates likely means you'll need to redo the tweaks.

    If you don't know what you're doing, don't do it
  3. Jeremy's Avatar
    This is an awesome post with some great tips. Thanks for posting all this.
  4. KevinMcC's Avatar
    "PS converts gif to png and jpg to jpeg"

    Is there any difference in jpg and jpeg for speed or is that just the format google uses?
  5. MPC's Avatar
    Short answer: It seems liek they are the same thing.

    I found an explanation of the different extensions. JPEG stands for Joint Photographic Experts Group. JPG (pronounced jay-peg) is the most commonly used file extension used to identify files created with this lossy format, and is the same as .peg. Both JPEG (Joint Photographic Experts Group) and JPG (Joint Photographic Group) are bitmap compression formats for picture and image files with compression ratios ranging from 10:1 to 20:1. In fact, JPG and JPEF file formats are identical. Older DOS-based computers were designed to handle a maximum "3-character file extension" which is why JPG was attributed to compressed image files. Newer Operating systems such as Windows XP and Vista allow for longer file extensions as evidenced by ".html". Accordingly, the JPF file extension was upgraded to the JPEG file extension which is the true acronym for Joint Photographic Experts Group. Just as a side note, XP and Vista will also support the older JPG file extension.
  6. martin1's Avatar
    wao its great information for me because i have just started my graphic designing course..can you please explain GIF as you explain about jpeg.....???
    thanks in advance
  7. rake360's Avatar
    looks like a damned awesome post. Will monkey around with this sometime soon, but bookmarked for now. thx
  8. MAC's Avatar
    Awesome article MPC.

    Any tips found for 3rd party js files or images like analytics or gpwa/cap badges?
  9. sophie01's Avatar
    Hi just want speed up your site to the top just visit us on link provided below.
    =======
    seo services in india

Trackbacks

Total Trackbacks 0
Trackback URL: