Automatically Compressing JavaScript files on Apache

Compress (minify JS) is one of most important missions if you need to optimize your web performance. And using JavaScript compressors is the easiest way to do this task, I recently present some popular minify JS compressors in Performances of JavaScript Compressors.

However, this solution has a big advantage you should know: we have to compile our JavaScript source codes/files before uploading them to live server if we change the source code. In order to overcome this shortcoming, we can use an alternation: compressing JavaScript source codes/files automatically on our server then give back the results as requested.

That's subject of this JavaScript article tutorial: guides you how to compress JavaScript source codes/files on Apache.


Sampled by © JavaScriptBank.com

One of the most recommended (and easiest to implement) techniques for web pages optimization is compressing the CSS and JavaScript files used.

While this is not a particularly difficult thing to do manually (for example, using the YUI compressor found here), when releasing a new version on the live server, it would be much easier to use an automated method that allows you to write the JavaScript in your own development style (using comments and a more loose formatting), and serve it directly compressed to the browser, on request.

Well... wonder no more! We're going to present an easy and bullet-proof method of doing just that, on an Apache web server. For this purpose, we're going to use Dean Edwards JavaScript's Packer class, more precisely its PHP version, "translated" by Nicolas Martin.

You're going to have to add it to your JavaScript folder (assuming you have such a folder in your project, otherwise... add it in your common folder, or... wherever you want). In the same folder, you have to add an index.php file that will load this PHP class and use it to dynamically compress your JS files.

The content of the JS folder's index.php file should be something like

if(!empty($_GET['script']) && is_file($_GET['script'] . '.js')){
require_once '/relative/path/to/JavaScriptPacker.class.php';

$script = file_get_contents($_GET['script'] . '.js');

$packer = new JavaScriptPacker($script, 62, true, false); // Default configuration
$script = $packer->pack();

header('content-type:application/javascript');
echo $script;
}

This, as you may notice, will receive a script parameter (type GET) and, if it will fit a file in your JS folder named as its value (plus the .js extension), will compress and output it.

In order to automatically call the index.php page with the JS file's name (without the .js extension), you need some .htaccess commands. These are:

Options All -Indexes # Disabled direct access to your JS folder's content

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /path/to/your/JavaScript/folder/

RewriteRule ^(.*)\.js$ ?script=$1 [L] # Here you instruct the server to pass any JS file called directly, to index.php as its script parameter
</IfModule>

As you may see, you must have the mod_rewrite module installed and active for this method to work.

If you have followed and understood all of the above... you now should access ONLY compressed JS files when calling them directly from your address bar (for example, try accessing http://your.domain.name/path/to/your/js/file.js - you should see something beginning with

eval(function(p,a,c,k,e,d){ ...

So... that's it! You now serve compressed (and somewhat "encoded") JavaScript files, thus minimizing the traffic and the time needed for the browsers to load your scripts.

One way of seeing the effects of this change is using either Page Speed or YSlow Firebug plug-ins. Try testing your web page without this compression method, and then, with it enabled. Depending on the size of your original script(s), you should be able to increase your ratings between 2-3 and up to 10 points.

Of course, you could go one step further, and try concatenating all of your scripts (in case you use more than one - and this is usually the case), and THEN compress it - you should gain up to 15 points in the performance rankings. But... I'll let you do that yourselves.

Language
Translate this page to English Translate this page to French Translate this page to Vietnamese

Recent articles
Insights for Advanced Zooming and Panning in JavaScript Charts
How to open a car sharing service
Vue developer as a vital part of every software team
Vue.js developers: hire them, use them and get ahead of the competition
3 Reasons Why Java is so Popular
Migrate to Angular: why and how you should do it
The Possible Working Methods of Python Ideology
JavaScript Research Paper: 6 Writing Tips to Craft a Masterpiece
Learning How to Make Use of New Marketing Trends
5 Important Elements of an E-commerce Website


Top view articles
Adding JavaScript to WordPress Effectively with JavaScript Localization feature
Top 10 Beautiful Christmas Countdown Timers
Top 10 Best JavaScript eBooks that Beginners should Learn
65 Free JavaScript Photo Gallery Solutions
16 Free Code Syntax Highlighters by Javascript For Better Programming
Best Free Linux Web Programming Editors
Top 50 Most Addictive and Popular Facebook mini games
More 30 Excellent JavaScript/AJAX based Photo Galleries to Boost your Sites
Top 10 Free Web Chat box Plug-ins and Add-ons
The Ultimate JavaScript Tutorial in Web Design


Free JavaScript Tutorials & Articles
at www.JavaScriptBank.com