简体   繁体   English

将javascript文件版本化为maven构建的一部分,以避免Web浏览器缓存

[英]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. 我有一个java / spring网络应用程序应用程序,它使用相当多的javascript作为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. 然而,当我发布新版本时,我是手动的,很好地使用我的ide的重构工具,如果编辑它们,则重命名javascript文件。

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. 这可以避免用户遇到不兼容/过时的javascript文件,但不包含新功能......或者更糟糕的是使用更新的jsp / html。

Is there a better way to add versioning to javascript files and their scrip tag references ? 有没有更好的方法将版本添加到javascript文件及其脚本标记引用?

You could do something like this. 你可以这样做。 Which enforce the browser to load the new updated javascript. 其中强制浏览器加载新更新的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 每次更新javascript文件时,只需更新像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. 所以较旧的javascript文件不会被缓存。

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). 我使用它的方式,它在文件内容上做了一个md5(在缩小和编译所有文件之后)。

You end up with a file name looking like script.H6oylsUj9sWn.min.js for instance. 你最终得到一个文件名,例如script.H6oylsUj9sWn.min.js

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. 除此之外,我的编译器还生成我的服务器端脚本,将它们插入到我的html中。 You can of course automate all that. 你当然可以自动化所有这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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