Question

I use Minify to reduce the http request when HTML needs to load JavaScript or CSS. However, I would like to further reduce the http request.
The question is: Is it necessary using PHP to output content like this:

<?php
header('Content-Type:Application/x-javascript;Charset=utf-8');
echo '<script src="text/javascript>';
foreach ($files as $file) {
  echo file_get_contents();
}
echo '</script>';
?>

What is the difference between using <script> tags and PHP output?

Any answer will be helpful, Thanks!

Was it helpful?

Solution

Take a look on Expiration Headers. With this approach your client (browser) will load JS/CSS files only for the first time and then it will keep them in cache

Example how to do it with .htaccess file: https://github.com/h5bp/html5-boilerplate/blob/dac15682b35ad69f519205e1b82694d0cab189ca/.htaccess#L192

Keep in mind, that when clients store your file in cache they won't send requests for this file again till it expire - even if you change content in this files. The best approach is appending something to file name every time when you change them, for example :

<script src="path/to/js/file.js?t={TIMESTAMP}" type="text/javascript"> </script>

where {TIMESTAMP} is a timestamp of last deployment/file modification

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top