Browser caching is the ability of a browser of storing results from remote resources. The process if fairly simple: it remembers the url
the resource was requested from and the response. If, while the resource is cached, the resource is requested again, rather than making the call, the browser serves the saved copy from cache, as it saves bandwidth and time.
If you add a parameter that is always unique to a resource call, the browser will always reload it, because the parameter will be changed and the browser will assume it's a different resource.
Typically, a timestamp
in either seconds (php timestamp) or milliseconds (javascript timestamp) will make sure your resource will always be reloaded:
JavaScript:
<script src id="myScript"></script>
<script type="text/javascript">
// change path to match your file:
let resourcePath = '/js/someScript.js';
document.getElementById('myScript').src = resourcePath + '?v=' + Date.now();
</script>
PhP:
<script src="/js/someScript.js?v=<?=time();?>"></script>
Note: you can do the same for any other resource (.css
or media resources), to disable caching. Also note you're not, technically, disabling caching - that's not so easy to do and differs from browser to browser. You are allowing caching but you're always requesting a different resource, because it has the bogus parameter which keeps changing (and which could be renamed from v
to anything else, for example, to ?no-cache=
).