问题
I have a quite troublesome issue which I didn't find a good solution for yet.
I allow caching of all my application static files (JS, CSS and images) by browser for performance.
Problem is, when I'm doing upgrades, the users still use the old version from their cache, which often breaks the application, and requires clearing the cache every time to solve the problem.
Is there any good multi-browser approach which still allows caching the files, but can force to reload them when needed?
Thanks for any info.
回答1:
Append somethign to the URL as parameter, e.g. myresource.css?version=1
. The file will be servered correctly, that's a trick to force reloading the cache. You only need to generate the html page dynamically.
回答2:
I've heard talk of using version numbers in the filenames you want you users caching, but I can't speak to how "good" that is compared to other options.
回答3:
Options I can think of:
- Reduce the cache TTL
- Store the cacheable stuff in a versioned directory tree, e.g.:
- /app/1.2/css/
- /app/1.2/js/
回答4:
You can use below format for auto random versioning
for js file:
<script type="text/javascript" src="js/app.js?seq=<%=DateTime.Now.Ticks%>"></script>
and for css file:
<link rel="stylesheet" type="text/css" href="Styles/styles.css?seq=<%=DateTime.Now.Ticks%>" />
来源:https://stackoverflow.com/questions/2050736/preventing-browser-caching-on-web-application-upgrades