Question

Okay, So I've googled a couple of pages and come up with the following solution:

Add this in .htaccess
#Active GZIP for php-files
php_flag zlib.output_compression On 
php_value zlib.output_compression_level 5

and this for .js and .css:

AddHandler application/x-httpd-php .css .js
php_value auto_prepend_file /Library/WebServer/Documents/blog/code/Helpers/ContentHeader.php

Which will execute .js as well as .css as php scripts. The second row includes the following page:

<?php
    $pathInfo = pathinfo($_SERVER['PHP_SELF']);
    $extension = $pathInfo['extension'];

    if($extension == "css")
    {
        header("Content-type: text/css");
    }
    if($extension == "js")
    {
        header("Content-type: text/javascript");
    }
?>

Which will send header "text/css" to .css files and "text/javascript" to javascript files. So far so good. The problem however is with a couple of minified javascripts that I use I get some kind of php error:

<br />
<b>Parse error</b>:  syntax error, unexpected ']' in <b>/Library/WebServer/Documents/blog/public/scripts/prettify/prettify.js</b> on line <b>18</b><br />

or:

<br />
<b>Warning</b>:  Unexpected character in input:  '\' (ASCII=92) state=1 in <b>/Library/WebServer/Documents/blog/public/scripts/showdown.js</b> on line <b>29</b><br />
<br />
<b>Parse error</b>:  syntax error, unexpected '?' in <b>/Library/WebServer/Documents/blog/public/scripts/showdown.js</b> on line <b>29</b><br />

So, is there any better way of doing this? The only option I can come up with is to manually go in to the .javascript files and "correct the errors" that php doesn't like. But shouldn't it be a easier solution?

Was it helpful?

Solution

The reason you're getting this error is that the scripts contain <?. For example, in prettify.js is this:

/^[^<?]+/

You're passing this into PHP, so that starts the PHP engine going again and sees an unexpected ].

I'd go with @jonstjohn's method, or @cletus's if you need more control.

OTHER TIPS

We use Apache Mod_Deflate and use the example configuration shown there. Works great without hacking in PHP.

I actually serve my JS files from a PHP script. Not because of this issue but so I can control the expires header and caching. The general model is:

  • The URL is something like /script/blah.1234567890.js;
  • That URL is generated from the mtime of the blah.js file;
  • That URL is sent to a js.php script via a rewrite rule in the .htaccess;
  • The script checks the mtime passed in. If its newer than the mtime of the cached version it generates a new cached version from all my JS files (that are collated into a single file and then minified with JSMin);
  • If the mtime is not newer than the script, the cached version is simply served;
  • In all cases the content is set to expire a year from now.

The timestamp in the URL allows for refreshing the script file.

Now how does this relate to gzipping? Simple. At the top of my js.php script is:

ob_start("ob_gzhandler")

which turns on gzip compression if the client supports it (as well as using output buffering).

It works really really.

Try and limit yourself to exactly one JS and one CSS file. Combine them if necessary. I do the same trick with CSS files too (no minification of course).

The advantage of this solution is it only relies on mod_rewrite being enabled. No other extension is necessary (ob_gzhandler is a standard part of PHP). Plus a script is necessary to combine the files (unless you do that as part of a build process but I find this a bit more tedious).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top