I was thinking about creating script that would do the following:
- Get all javascripts from JS directory used on server
- Combine all scripts to one – that would make only one request instead of multiple
- Minify combined script
- Cache the file
Let’s say that the order in which the files need to be loaded is written in config file somewhere.
Now when I load myexamplepage.com I actually use jQuery, backbone, mootools, prototype and few other libraries, but instead of asking server for these multiple files, I call myexamplepage.com/js/getjs and what I get is combined and minified JS file. That way I eliminate those additional requests to server. And as I read on net about speeding up your website I found out that the more requests you make to server, the slower your web become.
Since I’m pretty new to programming world I know that many things that I think of already exists, I don’t think that this is exception also.
So please list what you know that does exactly or similar to what I described.(please note that you don’t need to use any kind of minifiers or third party software everytime you want your scripts to be changed, you keep original files structure, you only use class helper)
P.S. I think same method could be used for CSS files also.
I’m using PHP and Apache.
Rather than having the server do this on-the-fly, I’d recommend doing it in advance: Just concatenate the scripts and run them through a non-destructive minifier, like
jsminor Google Closure Compiler in “simple” mode.This also gives you the opportunity to put a version number on that file, and to give it a long cache life, so that users don’t have to re-download it each time they come to the page. For example: Suppose the content of your page changes frequently enough that you set the cache headers on the page to say it expires every day. Naturally, your JavaScript doesn’t change every day. So your
page.htmlcan include a file calledall-my-js-v4.jswhich has a long cache life (like, a year). If you update your JavaScript, create a new all-in-one file calledall-my-js-v5.jsand updatepage.htmlto include that instead. The next time the user seespage.html, they’ll request the updated file; but until then, they can use their cached copy.If you really want to do this on-the-fly, if you’re using apache, you could use
mod_pagespeed.