Preventing browser caching on web application upgr

2019-04-09 04:01发布

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.

4条回答
在下西门庆
2楼-- · 2019-04-09 04:16

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.

查看更多
趁早两清
3楼-- · 2019-04-09 04:27

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.

查看更多
男人必须洒脱
4楼-- · 2019-04-09 04:31

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%>" />

查看更多
别忘想泡老子
5楼-- · 2019-04-09 04:34

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/
查看更多
登录 后发表回答