Let’s say I have a javascript or CSS file that is only used on one or very few pages on a site. I also have a method for easily identifying when those pages are served, such as a human-readable URL. Is it bad practice to conditionally serve the JS using a server-side test to see if the script is necessary? With PHP for example, I might do this:
<?php
if($page='myFancyPage')
{
?>
<script src="myFancyScipt.js"></script>
<?php
}
?>
I’m not losing the benefits of browser caching am I? Are there are other reasons why I shouldn’t do this?
I recognize that is might get unwieldy as a site gets large and more and more conditionals have to be piled up. But aside from that issue, are there reasons to avoid this practice?
No because the condition is on the SERVER not the CLIENT.
The client has no idea why it got the file, but when it gets it, it will be cached.