Welcome to HTTP REQUESTS hell. Concatenate javascript files!

I often find myself looking through other websites sources, and find something like this :
Mereu mă uit prin sursa siturilor pe care intru și dau de așa ceva:
screen

That, my friends causes the webserver to answer 8 requests everytime you load a page. For static content, of course. Now you tell me why on earth don’t people concatenate those files into a single output to save the webserver the extra work to response 8 requests when it can only answer one.
This bugged me ever since we started having problems caused by the abundance of users at craiovaforum which was causing apache to crash because of too many requests. Why not save 7 requests by simply concatenating the js files together?
Ok you might ask : “But I want to modify them , why have a single file to work on?”
The answer is simple and I made a simple php script for you:

< ?php
  $dir = "./js";
  if(is_dir($dir)){
    foreach(scandir($dir) as $file) {
      if(substr($file,-3,3) === '.js') {
	echo file_get_contents(realpath($dir.'/'.$file));
      }
    }
  }
?>

This way you simply add a <script src=”http://yoursite.com/script.php” type=”text/javascript”></script> and voilá.
Please, save the webservers!

5 Responses to “Welcome to HTTP REQUESTS hell. Concatenate javascript files!”

  1. I would go even further by caching those files into a single big JS file. Actually I did that with one of the projects I am taking care of, with both the JS and the CSS files. In order to avoid loading extra stuff, there are multiple JS/CSS cache files, served for each specific context, but there are no more that a couple of requests per page for this specific task. The browser cache issue is solved by versioning the cached files with a timestamp. The Yahoo! Dev speed guide is a golden resource for this kind of scalability issues.

  2. Mi-a placut articolul asta despre Welcome to HTTP REQUESTS hell. Concatenate javascript files!. Ai facut o treaba buna JohanTe invit si pe blogul meu. E nou, dar e ok

  3. Nice post Welcome to HTTP REQUESTS hell. Concatenate javascript files!. Mi-a placut articolul asta Johan. Nice job

  4. Hai ca mi-a placut Opera 10: the future is very bright. Imi place modul in care scrii Johan. I like this style

  5. E o solutie buna si concatenarea fisierelor dar tot trebuie sa incarci continutul respectiv si asta poate sa dureze un pic. Chiar sunt necesare toate acele scripturi pe fiecare pagina?

Leave a comment