When I have bug in javascript file I fix it and deploy. But to have updated file in browser I should press Ctrl+F5.
It is not an issue for me, but it is difficult to say to user to update files.
How to make user browser to update javascript file from server without special actions?
One of the approaches is to add to path to js file revision of file - every time file path will be different. But this is not convenient.
Always assert your assumptions with good tests. A client wanted to know how to 'force' a client to update some javascript that the browser had cached. The easy answer is "change the file." Here's what happens with a single HTML file and a single JavaScript file, running locally on my machine.
Our current thought is to simply attach a version number onto the name of the JavaScript files and then when changes are made, increment the version on the script and update all references. This definitely gets the job done, but updating the references on each release could get cumbersome.
The common practice nowadays is to generate a content hash code as part of the file name to force the browser especially IE to reload the javascript files or css files. It is generally the job for the build tools such as webpack. Here is more details if anyone wants to try out if you are using webpack.
I'm not sure if the same behavior is true for Javascript, it may not be. But if there's anything I learned, I never rely on the client. Even between the two of us there's some differences in behavior that could be disasterous (relatively speaking, of course) if clients were so unpredictable.
What many sites do is add a URL parameter on the URL pointing to the javascript file and change it every time that the file changes.
Something like this:
<script langauge="javascript" src="myfile.js?ver=1" />
And after a change:
<script langauge="javascript" src="myfile.js?ver=2" />
It may not be convenient, but it is the most common solution. Some web frameworks automate this process so the developer does not have to manually increment the version number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With