admin管理员组文章数量:1424893
I have a dynamically-generated javascript file that I want to ensure is NEVER cached by the browser. My current method is to simply append a (new) guid to the url in the script tag on each page view.
For example:
<script type="text/javascript" src="/js/dynamic.js?rand=979F861A-C487-4AA8-8BD6-84E7988BD460"></script>
My question is...is this the best way to acplish my goal?
For reference, I am using ASP.NET MVC 2 and the javascript file is being generated as a result of a controller action.
I have a dynamically-generated javascript file that I want to ensure is NEVER cached by the browser. My current method is to simply append a (new) guid to the url in the script tag on each page view.
For example:
<script type="text/javascript" src="/js/dynamic.js?rand=979F861A-C487-4AA8-8BD6-84E7988BD460"></script>
My question is...is this the best way to acplish my goal?
For reference, I am using ASP.NET MVC 2 and the javascript file is being generated as a result of a controller action.
Share Improve this question asked Jul 17, 2010 at 10:27 DanPDanP 6,4884 gold badges44 silver badges69 bronze badges6 Answers
Reset to default 6The method you have works just fine, an alternative would be to use DateTime.UtcNow.Ticks
. You can of course use cache control headers, but if you want to be absolutely sure, the method you have is the way to go.
Though, if you're generating the file dynamically anyway, and need to include it in the page...any reason for not just sticking the JavaScript in the page? Since you want it to never cache, this will save a round-trip for the client.
You could create a custom action attribute:
public class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
var cache = filterContext.HttpContext.Response.Cache;
cache.SetExpires(DateTime.UtcNow.AddDays(-1));
cache.SetValidUntilExpires(false);
cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
cache.SetCacheability(HttpCacheability.NoCache);
cache.SetNoStore();
base.OnResultExecuting(filterContext);
}
}
And then decorate the controller action that generates javascript with this attribute:
[NoCache]
public ActionResult MyAction()
{
...
}
In this case you no longer need the random string you are appending.
You might consider using HTTP Etags
That's a usual way how people do it. You can also change the filename for each requests to be pletely sure.
something like <script src="{unique string}.js">
Yes that is good. I personally do the same but I use timestamp instead which makes sure that it is always unique.
Use the cache control HTTP headers in the response.
Cache-Control: no-cache
(This is therefore purely a server setting, no coding needed.)
Alternately, as you are using ASP.NET MVC, use routing to ignore the last element of the URL, in the HTML have
<script type="text/javascript" src="/js/dynamic.js/979F861A-C487-4AA8-8BD6-84E7988BD460"></script>
and use the MVC route to ignore the suffix (i.e. make every HTML page served have a different script URL, not just a query parameter).
本文标签: aspnet mvcHow can I ensure a dynamicallygenerated javascript file is never cachedStack Overflow
版权声明:本文标题:asp.net mvc - How can I ensure a dynamically-generated javascript file is never cached? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745404650a2657196.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论