I have a dynamically-generated javascript file that I want to ensure is NEVER cached by the browser. My current method is to simply append a (new) guid to the url in the script tag on each page view.
For example:
<script type="text/javascript" src="/js/dynamic.js?rand=979F861A-C487-4AA8-8BD6-84E7988BD460"></script>
My question is…is this the best way to accomplish my goal?
For reference, I am using ASP.NET MVC 2 and the javascript file is being generated as a result of a controller action.
The method you have works just fine, an alternative would be to use
DateTime.UtcNow.Ticks. You can of course use cache control headers, but if you want to be absolutely sure, the method you have is the way to go.Though, if you’re generating the file dynamically anyway, and need to include it in the page…any reason for not just sticking the JavaScript in the page? Since you want it to never cache, this will save a round-trip for the client.