I have the following PHP file:
<?php
Header('Content-Type: application/javascript');
echo "// Some dynamically generated JavaScript here...";
So it's a .php file but it's interpreted as a JavaScript file. I would load it in a web page like so:
<script type='text/javascript' src='myjavascript.php'></script>
My question is regarding the HTTP code that is sent when accessing this file. Every time I request this file (either directly or via a webpage that is requesting it) Chrome Dev Tools shows a Status of 200 OK. It is never a 304, even if the content hasn't changed.
Is it possible to get a 304 Not Modified for a dynamically generated file like this in the case that the content hasn't changed? If not, why?
I have also used some PHP framworks that allow for routes like: /js/myjavascript.js
. So I can put the above code in that route, dynamically generating the JavaScript in the same way. But again, still a 200 every time, even though the content hasn't changed AND the URI is a .js file.
Is there a solution for this? In my situation, the contents of this javascript/php file changes about once a day. So I need my visitors browsers to cache the file most of the time (304 Not Modified) but when it does change, I need their browser to download and cache the new version (200 OK).