NimChimpsky
NimChimpsky

Reputation: 47290

versioning javascript files as part of maven build to avoid web browser caching

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 ?

Upvotes: 3

Views: 1862

Answers (2)

Suman Bogati
Suman Bogati

Reputation: 6349

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.

Upvotes: 7

Skwal
Skwal

Reputation: 2160

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.

Upvotes: 4

Related Questions