One major "best practice" from both Yahoo! YSlow and Google Page Speed is minimizing HTTP requests. Yahoo! indicates that "80% of the end-user response time is spent on the front end. Most of this time is tied up in downloading all the components in the page: images, stylesheets, scripts, Flash, etc." cite
What does this mean? The fewer calls your page makes to the server, the faster your page is. Here are three ways to drastically improve your site response time:
Here's one way to accomplish this:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="load.php?type=css">
</head>
<body>
... website content ...
<script type="text/javascript" src="load.php?type=js"></script>
</body>
</html>
<?php
/**
* The following directory structure is assumed:
* css/
* reset.css
* style.css
* js/
* jQuery.js
* main.js
* index.php
* ... (the rest of your webpages)
* load.php (THIS code block)
*/
error_reporting(0); // supress errors (browsers will see them as invalid CSS/JS)
ob_start('ob_gzhandler'); // compress with GZIP
$isJavaScript = ($_GET['type'] == 'js');
if ($isJavaScript) {
$type = 'javascript'; // content type
$ext = 'js'; // file extension
$files = array( // TODO: modify this list based on your own JS files
'jQuery',
'main'
);
} else {
$type = $ext = 'css'; // content type is the same as the extension
$files = array( // TODO: modify this list based on your own CSS files
'reset',
'style'
);
}
header("Content-Type: text/$type"); // set the appropriate content type
$cache = "$ext/cache.$ext"; // for example, css/cache.css
if (!file_exists($cache)) { // see if we've already created a cache
$toCache = '';
foreach ($files as $f) {
$f = "$ext/$f.$ext"; // for example, css/style.css
if (file_exists($f)) {
$c = file_get_contents($f);
if ($isJavaScript) {
include_once('jsmin.php'); // get JSMin here
$toCache .= JSMin::minify($c); // slow, but only called when there is no cache
} else {
// quick and easy way to minify CSS, removes multiple spaces and blank lines
$toCache .= preg_replace(array('/\s\s+/', '/\n/'), array(' ', ' '), $c);
}
}
}
file_put_contents($cache, $toCache);
}
readfile($cache);
exit;
?>
The first time the script is called (ever), it may take a few seconds to generate the cache file, but every time after that it will load extremely quickly due to the "three Cs" implemented above. Since minification is part of the process, you can work directly with unminified and commented code on the server, and let the script minify it for you. If you normally load 3+ stylesheets or scripts, you will see a massive performance boost with this type of script.
If and when you change any of your CSS or JavaScript, just delete the cached file and everything is automatically updated on your site's next visit.
Do you have a favorite method of combining scripts to improve website performance? Please share it in a comment!
I'm a Front-End Engineer at Yahoo! working on the Mail and Messenger teams. I blog about web design and development topics including accessibility, usability, performance, and developing HTML / CSS / JavaScript applications on Appcelerator Titanium and Adobe AIR.
If you're a web developer, you might enjoy Jelo, my JavaScript library.
A few panoramic shots I took at SDCC 2010. #geek http://bit.ly/bwX6GB
JS version of Regex prime number checker:
function isPrime(n) {
return Array(n + 1).join("1")
.search(/^1?$|^(11+?)\1+$/) == -1;
}
Погрузился в пучину EcmaScript5, местами увлекательно, местами нудно =)
Modernizr http://ow.ly/18njQ1
A Collection of 20 HTML5 Video Players - a round-up of JavaScript and html5 alternatives to Flash-based media player... http://ow.ly/18njQ2
jQuery TOOLS - The missing UI library for the Web http://ow.ly/18njQ3
Contactable - A jQuery Plugin | the odin http://ow.ly/18njQ4
Giants vs Dodgers, sweet seats. http://twitpic.com/2ag9pa
@snookca That'll be fixed next week. I promise.
@snookca I was tryna not name names ;) But really that was just par for the course today, pretty hectic day. As I'm sure you know.
Who breaks major stuff after 4pm on Friday? On the last day of the sprint, no less. Tsk. (wasn't me)
Awesome live git tracker for teams: http://www.utsup.com/
RT @DerrenBrown: Blog post: Camera Software Lets You See Into the Past http://bit.ly/9kjVg5
10 invites to the new version of Digg: http://bit.ly/dqM8EV
Threaded vs Evented Servers, great look at the whats and whys. http://bit.ly/bDUEjn #geek
Nav, Context menus, "app-style" toolbars in sample chapter http://bit.ly/csTRY8 of new YUI book http://bit.ly/cJINoV
Add a side-mounted End Call button to your iPhone 4: http://bit.ly/cGxPBD #funny #geekAll original work on this site is covered by a Creative Commons Attribution 3.0 license unless otherwise specified.
You may share or use any code or images from this site in any manner, for free, so long as reasonable effort has been made to give credit where due.
The views expressed in the posts and comments on this blog do not necessarily reflect the views of Yahoo!