views:

135

answers:

2

I'm using the PHP script for JSMin to remove comments and whitespace from my javascript and css files before serving. If I set

<script src="jsmin.php?file=myfile.js">

the file is not ever cached, since the Last-Modified HTTP header is never checked for a script. So in order to run the files through JSMin and (hopefully) enable caching, I'm using .htaccess with the following settings:

RewriteEngine on
RewriteRule ^.*$ jsmin.php?file=%{REQUEST_FILENAME} [NC,L]

I can then use:

<script src="myfile.js">

and get the effects of JSMin.

Apache still doesn't serve the file with the Last-Modified header, so it is still not cached by the browser. Is there a way I can look at the If-Modified-Since header in Apache and only serve the JSMin script if the file in question has been modified since the appropriate time? Otherwise, I'd like the browser to use a cached version.

A: 

Why don't you compress the file before deploying? Compressing it on runtime seems more complex and slower.

I use http://developer.yahoo.com/yui/compressor/

cherouvim
I've considered this, but the files change fairly frequently, so it's annoying additional work for our team with each update. The server overhead from running JSMin should be pretty much negligible, although if it became a problem this seems like the right approach.
Travis
You can setup a build process (lets say by using ant) and the whole distributable will be prepared for you with the press of a button. It makes things easier, faster and with less errors.
cherouvim
+3  A: 

+1 for a build process. Deployment should be automated. This makes the outcome predicable.

Also, Using a file path like that in a url is asking for someone to de-reference it and load a file they're not supposed to. Consider:

<script src="jsmin.php?file=../../../../../../../../../../../../../../etc/passwd">

If your script is just passing that right along to the command line your in trouble.

txyoji