简体   繁体   中英

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 ?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM