0

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?

1
  • If you are directly accessing "example.php", then no; it will be reloaded when you open that file. With that said, caching is a good thing. I would not recommend preventing caching of JS files outside of a development build unless you have a good reason. Commented Oct 20, 2021 at 8:06

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

good points here. I’d just add that it is possible to have a cached Html page without caching one or more of its dependencies. For instance the JS can be served with an always refresh policy, even if the html has been served from the cache
@Juan Yes, good point, I've added a bullet to clarify that JS in the page is not the same as JS referenced from the page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.