Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update javascript file in client browser?

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.

like image 895
sergtk Avatar asked Mar 27 '11 20:03

sergtk


People also ask

How to'force'a client to update some JavaScript that is cached?

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.

How do you update the version of a JavaScript script?

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.

How do I force a browser to reload a JavaScript file?

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.

Do you rely on your client for JavaScript development?

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.


1 Answers

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.

like image 179
Oded Avatar answered Nov 15 '22 03:11

Oded