Friday, June 13, 2008

Simple JavaScript/CSS gzipping

I'm sure some may already do this, but compressing textual content can save bandwidth but also make it easier to maintain (by just including a php file instead of a CSS or JavaScript one). For example, create a file jquery.php in the same directory as JavaScript files:

<?php
ob_start("ob_gzhandler");
header("Content-Type: text/javascript");
include("jquery-1.2.6.min.js")
?>

Then include it instead of the normal JavaScript file:

<script type="text/javascript" src="/js/jquery.php"></script>

It's simple to maintain and could be improved (for example, some kind of caching). CSS files can also be used:

<?php
ob_start("ob_gzhandler");
header("Content-Type: text/css");
include("styles.css")
?>

No comments: