views:

77

answers:

4

So my application uses a LOT of js files. thats a lot of http requests. I decided to combine them dynamically at the server in packs of 3-4 files clubbed by functionality.


My client side request is: ...script type="text/javascript" src="http://mydomain.com/core-js.php" ...

My server side does: --core-js.php-- header("Content-type: application/x-javascript");

include_once('file1.js'); include_once('file2.js'); include_once('file3.js'); include_once('file4.js');


I am setting a far future expire header on core-js.php. My question is, would core-js.php be cached at the client side? If it would be, could someone please explain how?

Thanks!

+3  A: 

The client doesn't know or care that what got sent to it was satisfied by bringing together several files server-side. The client should cache it if the caching headers are correct. You'll want to check them carefully to be sure that your PHP install isn't sending other headers that conflict (Firefox+Firebug is good for this), since PHP pages tend to be used for dynamic stuff where you don't want caching.

T.J. Crowder
A: 

You have to implement a validation mechanism for the client side cache. You may use my class HTTP_Response as an example for this.

Usage:

$headers = new HTTP_Response(false);
$headers->set_mime('application/x-javascript');
$headers->send();
toscho
I am not sure I understand your response.
Grae
What type of validation?
Grae
Compare the client’s Last-Modified or ETag value with the local one. If both are equal, return 304.
toscho
A: 

The vast majority of browsers and caching proxies will respect the expiry header (if set).

Jamza
A: 

Yes it will. The client doesn't know that the js file he's requesting is a bunch of other files chunked into one, he's just seeing one js file, the one he requested and it's telling him to cache it, core-js.php. As long as you don't change the name of the file (core-js.php) there should be no problem.

On another note, you should take a look at Minify http://code.google.com/p/minify/ You can merge and cache not only js but css in groups, basically what you're doing. I've been using it for a while with no problems and it's pretty nice.

Ben