I have seen numerous references to gzipping a javascript in order to save download time. But I additionally see numerous alerts that particular browsers don't support this.

I've two different techniques inside my disposal:

  1. use mod_deflate to create Apache compress JS/CSS files inside a given directory through htaccess
  2. use doctor_start('gzhandler') to compress personal files and give it back towards the browser using the correct headers.

The issues with method 1 are that does not all browsers support mod_deflate, and that i have no idea crafting the htaccess file to become wise enough to regulate with this.

The issue with method 2 is the fact that there's no definitive answer about how exactly to inform if your browser supports a gzipped script, or stylesheet, which whether it does what mime-type should be given because the content enter in the header.

I want top tips. First, which technique is more globally recognized by browsers? Second, how do you decay using either approach to supply the uncompressed backup script? Third, would <script src="js/lib.js.gz" type="text/javascript"></script> work alone? (It clearly wouldn't decay.)

For that record, I am using PHP5 with mod_deflate and full gzip creation abilities, and my doctype is xhtml strict. Also, the javascript is compressed with YUI. Edit: I simply returned and looked, however i only have Apache 1.3 I figured I'd 2, so sorry for mentioning mod_deflate after i most likely do not have it.

mod_deflate and php's gzhandler both derive from zlib, so for the reason that sense there's little impact on a browser the way the submissions are being compressed.

in reaction for your main objective, you are able to set module specific .htaccess info such as this:

<IfModule mod_deflate.c>

  # stuff


in reaction for your second concern, you are able to identify for browser support in PHP:

if (strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') ) doctor_start('ob_gzhandler')

  header("Content-Encoding: gzip")

// etc...

here's some untested .htaccess which should have the ability to handle settlement of compressed versus uncompressed .js files: (source)

<FilesMatch ".js.gz$">

  ForceType text/javascript

  Header set Content-Encoding: gzip


<FilesMatch ".js$">

  RewriteEngine On

  RewriteCond % !".*Safari.*"

  RewriteCond % gzip

  RewriteCond %.gz -f

  RewriteRule (.*).js$ $1.js.gz [L]

  ForceType text/javascript


The easiest method to achieve to your server to aid inline gzip. You'd go ahead and take ".gz" off both script tag's src attribute and keep file around the server uncompressed. When the client supported it, the server would instantly send the script like a gzip encoded result. This may cost your server additional CPU, however the file will get compressed for individuals clients that support it, while older clients get the broadened version.

Take a look at mod_deflate for apache2. mod_gzip may be the equivalent for Apache 1.x.

When the browser is correctly setting it's Accept-Encoding header, you need to have the ability to know if the browser supports gzip:

Accept-Encoding: gzip,deflate