views:

40

answers:

2

Hi guys, I'm trying to optimize my web application and unfortunately have ended up with a javascript file size of around 450K - that too after compressing [it would take a while for me to redo the javascripting but until then I have to go live] - I initially had made a number of small javascript libraries to work upon. And what I do is I have a php file which includes all the javascript files and then I included my php file as below:

<script language="js/js.php"></script>

The thing is that I was hoping that my file would be cached upon the first load but it seems every time I refresh the page or come back to it the file is reloaded from the server - I checked this using firebug. Is there anything else that I must add to ensure that my file is cached on the user end.. or am I misunderstanding the idea of a cache here?

+3  A: 

You'll need to set some headers in php to ensure the file is cached.

At the top of js.php put:

ob_start("ob_gzhandler");

$expires = 2678400; // 1 month in seconds
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');

That will add both basic caching + gzip compression on the fly.

Ben Rowe
also, he may want to change the script tag to be valid, `<script type="text/javascript" src="js/js.php"></script>`
Alexander Gyoshev
Worked like a charm :D Thanks for that!
Ali
A: 

Why not to leave it .js file and let web-server take care of caching?
Compression is not the thing you really need but Conditional Get is

Col. Shrapnel