Js CSS img and other browser cache problems of the two solutions

  • 2020-03-26 21:40:24
  • OfStack

The devil is in the details! The significance of browser caching is to improve the execution efficiency, but it also brings some problems, resulting in the server side modified js, CSS, the client side can not be updated
Method 1 generates random Numbers
 
<script type= " text/javascript "  src= " /js/test.js?+Math.random() " ></script> 

On the downside, the browser cache loses its meaning and loads dynamically every time.

Method 2 version number control, set global variable, before each release version number plus 1
 
<script type= " text/javascript "  src= " /js/test.js?v=<%=v%> " ></script> 

I recommend it. It's easy. The downside is that HTML pages are not usable, but there shouldn't be too many of them

More methods: to read the file size, file modification time to generate MD5 way to automate the implementation, the program automatically built when the compression merge js and global replacement version, the way is complex, interested can be studied

Who has a better way?

Related articles: