最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Elegant way to force client browsers to re-download our asp.net web app's .css and .js files (without total

programmeradmin0浏览0评论

Our asp 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...?

Our asp 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...?

Share Improve this question edited May 23, 2017 at 11:49 CommunityBot 11 silver badge asked Jul 13, 2013 at 3:56 MGOwenMGOwen 7,34713 gold badges60 silver badges72 bronze badges 1
  • What is the solution that you reached? Can you please post? – LCJ Commented Nov 6, 2013 at 13:14
Add a ment  | 

3 Answers 3

Reset to default 4

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.

You can now append asp-append-version="true" to a <script> or <link> tag to solve your problem automatically.

<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />

<script src="~/js/index.js" type="text/javascript" asp-append-version="true"></script>

Short article explaining usage

Microsoft Reference


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 bee somefile_v1321.js or somefile.js will bee 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论