Home > Blockchain >  Auto-Versioning inline Javascript in a PHP File?
Auto-Versioning inline Javascript in a PHP File?

Time:10-21

I have the following question / problem: I know that if I append something like "?v=*" in my Javascript includes such as <script src="/js/myJsFile1.js?v=1.1" type="text/javascript"></script> I can force a client side recaching.

Now my question is, if I have a fragment such as the following in an example.php...

<?php ... ?> 
<script> 
    var a = 5; 
    callRandomFunction(); 
</script> 

...is there a need to have a version-control for this fragment as well? As it is included in a PHP File and therefore always called from the server anyway, does a forced caching make sense here?
If it does - Is there some similar way as there is with the "?v=" appendix?

CodePudding user response:

A few points of confusion:

  • "caching" refers to re-using content; the techniques you are talking about are not "forced caching", they are forcing the browser not to use a cached copy
  • the parameter doesn't mean anything to the browser, it just means that this is a URL the browser has never seen before, so it won't have a cached copy
  • the browser doesn't see that something is part of a PHP page, only that it's part of an HTML page
  • the browser doesn't cache parts of pages, so if that HTML page is cached, the JS inside the page is cached too; if it is not, it is not
    • to be clear, JS served on a separate URL but referenced in the HTML page will be cached separately, which is why changing the URL for each version makes a difference

So, the question comes down to:

Will my HTML page be cached?

The answer depends on cache headers, which are a surprisingly complicated part of HTTP. But the short version is that PHP will generally set headers telling the browser not to cache pages it generates.

So the really short answer is: no, you don't need to do anything.

  • Related