Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Javascript file with PHP

My current code

I have started writing an API that loads, minifies and returns javascript files into one file using PHP. This is achieved by pointing to a PHP file from a script tag in HMTL like so:

<script type="text/javascript" src="https://libraries.sinemaculammviii.com/jsapi.php"></script>

This jsapi.php page processes the javascript files and outputs the minified javascript, with the header:

header("Content-Type: text/javascript");

My Question

Is this a bad method to load javascript files? Would it be much faster and reliable to load the javascript files individually by simply pointing to the .js file in the src attribute?

If you wish to see my code for the the full API, have a look at this. The mentioned link also explains in detail what it is I am doing and why.

like image 636
Ben Carey Avatar asked Nov 05 '12 10:11

Ben Carey


1 Answers

Faster as .js yes, but marginally. The reason being that it would be a static file so wouldn't need the processing time PHP will take.

That said there's nothing wrong with feeding JavaScript through PHP like this. You could even come up with caching methods as well to reduce the processing impact.

like image 154
Lloyd Avatar answered Sep 18 '22 13:09

Lloyd