views:

1103

answers:

3

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?

+6  A: 

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

jonstjohn
Well I'm not sure that that solution would work on my shared host. Or maybe it does...The only thing I know is that my shared host is having zlib installed.. which is why I prefer to use that php extension
aktersnurr
I use mod_deflate under Apache as well.
R. Bemrose
Ok. But I'm not sure that this will work on my shared host. So I prefer to use zlib as that extension I know is installed on my host.
aktersnurr
+1. mod_deflate is easier to set up and will perform *much* better than a naive PHP file server (especially because Apache will get the caching right). It would be a poor host indeed that didn't have mod_deflate available.
bobince
+2  A: 

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).

cletus
Yeah, I also use the same kind of versioning system(almost) However I'm in doubt that adding ob_start("ob_gzhandler") on top of the .php (or.js file) will solve the issue, since php would still complain on the the same javascript line as stated in the question. I will try it though.
aktersnurr
+1  A: 

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.

Greg
True true. Thanks. I should've thought about that. I will contact my host to see if mod_deflate is enabled..
aktersnurr