简体   繁体   English

IIS7 ASP.NET MVC静态JavaScript文件缓存?

[英]IIS7 ASP.NET MVC Static JavaScript File Cache?

I have a really simple site that I created. 我创建了一个非常简单的网站。 I am trying to test JS caching in the browser but it doesn't seem to be working. 我正在尝试在浏览器中测试JS缓存,但似乎无法正常工作。 I thought that most major browsers cached your JS file by default as long as the file name doesn't change. 我认为大多数主流浏览器默认都会缓存您的JS文件,只要文件名不变即可。 I have the site running in IIS 7 locally. 我的站点在IIS 7中本地运行。

For my test I have a simple JS file that is doing a document write on body load. 对于我的测试,我有一个简单的JS文件,该文件在加载时进行文档写入。 If I make a change to the JS file (change the text the document write is writing), then save the file, I see that updated when refreshing the browser. 如果我更改了JS文件(更改了文档写的文字),然后保存了该文件,则刷新浏览器时看到更新了。 Why is this? 为什么是这样? Shouldn't I see the original output as long as the JS file name hasn't changed? 只要JS文件名没有更改,我是否应该看不到原始输出?

Here is the simple site I created to test. 这是我创建的要测试的简单站点

When you refresh your browser, the browser sends a request to the server for all the resources required to display the page. 刷新浏览器时,浏览器向服务器发送请求,以显示该页面所需的所有资源。 If the browser has a cached version of any of the required resources, it may send an If-Modified-Since header in the request for that resource. 如果浏览器具有任何所需资源的缓存版本,则可能会在对该资源的请求中发送If-Modified-Since标头。 When a server receives this header, rather than just serving up the resource, it compares the modified time of the resource to the time submitted in the If-Modified-Since header. 当服务器接收到此标头而不是仅提供资源时,它会将资源的修改时间与If-Modified-Since标头中提交的时间进行比较。 If the resource has changed, the server will send back the resource as usual with a 200 status. 如果资源已更改,服务器将照常以200状态发送回资源​​。 But, if the resource has not changed, the server will reply with a status 304 (Not Modified), and the browser will use its cached version. 但是,如果资源没有更改,服务器将以状态304 (未修改)答复,并且浏览器将使用其缓存版本。

In your case, the modified date has changed, so the browser sends the new version. 对于您而言,修改日期更改,因此浏览器将发送新版本。

The best way to test caching in your browser would probably be to use fiddler and monitor requests and responses while you navigate your site. 在浏览器中测试缓存的最佳方法可能是在浏览网站时使用提琴手并监视请求和响应。 Avoid using the refresh button in your testing as that frequently causes the browser to request fresh copies of all resources (ie, omitting the If-Modified-Since header). 避免在测试中使用刷新按钮,因为这经常会导致浏览器请求所有资源的新副本(即,省略If-Modified-Since标头)。

Edit: The above may be an over-simplification of what's going on. 编辑:以上可能是对正在发生的事情的过度简化。 Surely a web search will yield plenty of in-depth articles that can provide a deeper understanding of how browser caching works in each browser. 毫无疑问,网络搜索将产生大量深入的文章,这些文章可以更深入地了解浏览器缓存在每个浏览器中的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM