【问题标题】:squishit gives 304 (not modified) instead of 200 (from cache)squishit 给出 304(未修改)而不是 200(来自缓存)
【发布时间】:2012-05-20 15:27:27
【问题描述】:

我正在使用“SquishIt”来组合我的 .js 和 .css 文件。 问题是当所有其他资源为 200(来自缓存)时,squishit 捆绑包会导致 304(未更改)。如果我以常规方式放置文件,我会得到想要的 200 结果。 我的代码示例:

@MvcHtmlString.Create(@Bundle.JavaScript().Add("~/Scripts/Libs/jquery.cookie.js").Add("~/Scripts/Libs/jquery.dynatree.min.js").Add("~/Scripts/Libs/jquery.json-2.3.min.js").Add("~/Scripts/Libs/jsrender.js").Add("~/Scripts/Libs/jstorage.min.js").Add("~/Scripts/Common/utils.js").Add("~/Scripts/DataServices/AccountDataServices.js").Add("~/Scripts/AccountSelection.js").WithMinifier<SquishIt.Framework.Minifiers.JavaScript.MsMinifier>().Render("~/Scripts/AccSelectionscriptBandle_#.js"))

结果:

编辑: 我正在使用“调试=假”; 所有其他资源均为 200(来自缓存)

【问题讨论】:

  • 您必须查看标题,但我怀疑它使用 etag 进行缓存控制。 “未修改”状态意味着浏览器实际上正在使用缓存,但它会检查服务器以检查当前的 etag 值,以便知道它可以使用缓存的版本。
  • 谢谢,我在 web.config 中添加了&lt;staticContent&gt; &lt;clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="500.00:00:00" /&gt; &lt;/staticContent&gt; - 解决了问题

标签: javascript css bundle browser-cache squishit


【解决方案1】:

我相信您走在正确的轨道上,但您需要更进一步,并在您的 clientCache 设置中添加一个选项 (cacheControlCustom="must-revalidate"):

    <staticContent>
      <!-- A clients cache will expire 90 days after it is loaded -->
      <!-- each static item is revalidated against the server to see if the LastModifiedDate is different than the If-Modified-Since date-->
      <!-- http://msdn.microsoft.com/en-us/library/ms689443.aspx -->
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="90.00:00:00" cacheControlCustom="must-revalidate"/>
    </staticContent>

cacheControlCustom 强制对服务器进行检查,以比较特定静态文件的 lastModifiedDate 和 If-Modified-Since。设置 cacheControlMaxAge 只会让你到目前为止,特别是当你在服务器上修改文件时,你的客户端可能不会自动重新拉文件。您的设置(500 天)只会在文件在客户端计算机上保存 500 天后提取文件。如果这是你的意图,那就太好了。 (另外,SquishIt 会屏蔽一些缓存行为,因为更改包中的 js/css 文件会生成不同的哈希名称。)

评论中的 Url 可能会从 MSDN 中解释更多。

【讨论】:

  • 所以任何人都可以轻松找到它,the web.confg中的元素&lt;staticContent&gt;' lives in configuration/system.webServer`
猜你喜欢
  • 2013-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
  • 2017-04-04
相关资源
最近更新 更多