Moo.tools is neat, I love it and use it whenever I can, but one thing is still bugging me: its size.
Even though moo.tools can be downloaded compressed (using the great php5 version of dean edward’s packer), I personally find 18kb of javascript too much.
So I started researching a bit with Google and found out different way of compressing moo.tools (or actually whatever javascript you have) even further:
The first method, which I am using works like this:
+ You rename your javascript file (moo.tools) from the original filename.js to filename.js.php
+ Open the file with your favourite editor and add the following line just at the very TOP of the code:
< ?php if(extension_loaded('zlib')){ob_start('ob_gzhandler');} header("Content-type: text/javascript"); ?>
+ Next, scroll down to the end of the file in your editor and add the following to the bottom:
< ?php if(extension_loaded('zlib')){ob_end_flush();}?>
+ Save your file and upload it.
Now add it to your website using the old HTML code, but keep in mind to add the .php-file extension.
It might look like this:
I was able to reduce the whooping 18kb moo.tools down to 9kb this way. Well, actually I did not do anything, but the server and its php and zlib.
Other methods:
At the top of Javascript:
< ?php
ob_start ("ob_gzhandler");
header("Content-type: text/javascript; charset: UTF-8");
header("Content-Encoding: gzip");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 ;
$ExpStr = "Expires: " .
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
?>
.htaccess method
Save a compressed javascript file as *.jgz, and write .htaccess like this.
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ".*Safari.*" [OR]
RewriteCond %{HTTP:Accept-Encoding} !gzip
RewriteRule (.*)\.jgz$ $1\.js [L]
AddType "text/javascript;charset=UTF-8" .jgz
AddEncoding gzip .jgz
Filesmatch .htaccess method:
ForceType text/javascript
Header set Content-Encoding: gzip
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !".*Safari.*"
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule (.*)\.js$ $1\.js.gz [L]
ForceType text/javascript
Sources / Ideas:
1. paulstamatiou.com
2. randomnetworks.com
If you enjoyed this post, make sure you subscribe to my RSS feed!
Related posts:
- Wordpress Themeviewer opened again Hooray fo
Tagged with: code • compress • gzip • htaccess • javascript • moo • moo.fx • moo.tools • optimize • php • zlib



No comments
Jump to comment form | comments rss [?] | trackback uri [?]