3

I have a java/spring web app application that uses a fair bit of javascript as part of the gui. However when I release a new version I am manually, well using my ide's refactoring tool, renaming javascript files if they are edited.

This avoids users getting stuck with an incompatible/out of date javascript file that doesn;t include new functionality... or worse breaks with the newer jsp/html.

Is there a better way to add versioning to javascript files and their scrip tag references ?

2 Answers 2

7

You could do something like this. Which enforce the browser to load the new updated javascript.

yourscript.js?version=1234567890

On

<script type="text/javascript" src="yourscript.js?version=1234567890"> </script>

Every time you update the javascript file just update the number like yourscript.js?version=1234567891

How does it work?

Browser does interpret this as a new file every time when you'll update with new version number. So older javascript files would not cached.

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

3 Comments

Browser does interpret this as a new file every time when you'll update with new version number. So older javascript files would not cached.
Can you please tell where we need to set the version inside the javascript file?
@Lijo you don;t need to, it just stop sthe browser caching the result because the call to the js file changes.
4

A good practice is to insert a hash (or part of it) in the file name. The way I use it, it does a md5 on the file content (after minifying and compiling all files together).

You end up with a file name looking like script.H6oylsUj9sWn.min.js for instance.

That way, if the file didn't change, the hash stays the same and the client doesn't have to re-download the file.

Beside of that my compiler also generates my server side script that insert them in my html. You can of course automate all that.

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.