5

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.

4 Answers 4

12

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.

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

7 Comments

For example /path/to/javascript.js?123456789
thanks. just one drawback is to update all js file inclusions on deployment.
@sergdev - If you use an identical version number for all files, you can use search replace...
consider this use case: what if user is has open a webpage and is idle for 10 mins...in mean time, you update a JS file. How will you push the new JS file to client side without any action?
@Viral - I wouldn't, not this way. If this was a real requirement, one could do this with long polling of the server to check and replace the file (with appropriate checks that the client will not break when this changeover happens).
|
1

Add a random query string at the end, like SO does ;)

<script type="text/javascript" src="http://sstatic.net/js/master.min.js?v=d761eb59f537"></script>

Comments

0

The best way is to change name of file with javascript. I'm adding version number to do that: for example I'm changing scripts1.2.js to scripts1.3.js

Comments

0

just add any param to url

mysite/javascript/myscript.js?ver=1

and checnge this param with new version.

Comments

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.