Elegant way to force client browsers to re-download our asp.net web app's .css and .js files (without totally disabling caching)

北城余情 提交于 2021-02-10 09:23:48

问题


Our asp.net web application allows our (large) .css and .js files to be cached by client browsers for faster performance.

But whenever we deploy a new version, we get phone calls from users about how the page looks messy and is full of javascript errors. It turns out, their browser didn't re-download the changed .css and .js files. Ctrl-F5 always fixes it.

Is there any way to force a re-download after upgrade deployments, without setting it not to cache (and thus slowing our application down)?

I've found this (manually changing reference to every file every deployment): How do I force a given file to expire in the cache?

and this (same thing but checksum calculation on every page load): Needed advice on how to implement js/css versioning

But surely there are more reasonable solutions than that...?


回答1:


Setting the version number in the file works well for us:

<script type="text/javascript" src="somefile.js?v=1.0"></script>

As long as you change that version number each "release", the browser thinks its a new file.

You could do this by simply having a version number in a global file (like a config file or something) and then setting the variable on each script/css tag you wish.




回答2:



The best approach is to use the versioning in file name. If you are using code versioning like SVN, Microsoft Team Foundation etc, then you can use the last checkin number as suffix to file name, for example, let say the current checkin number is 1321 (or you can also use time stamp yyyy-mm-dd-hh-mm ie 2015-01-15-17-15)

therefore somefile.js will become somefile_v1321.js or somefile.js will become somefile_v2015-01-15-17-15.js

Now you will wonder there is so much file name change happening here. The answer is, if your releases are very frequent, then consider using scripting language like Ant scripts, which can change the JS and CSS import file names in release files.

Every time new deployment will take place, the resources will have a new unified path, which will be unique to the browser, so it will do fresh load.



来源:https://stackoverflow.com/questions/17626830/elegant-way-to-force-client-browsers-to-re-download-our-asp-net-web-apps-css-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!