squishit给出304(未修改)而不是200(来自缓存)

时间:2012-05-20 15:27:27

标签: javascript css bundle browser-cache squishit

我正在使用“SquishIt”来合并我的.js和.css文件。 问题是当所有其他资源都是200(来自缓存)时,squishit bundle会导致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"))

结果:

enter image description here

修改: 我正在使用“Debug = false”; 所有其他资源都是200(来自缓存)

1 个答案:

答案 0 :(得分:5)

我相信你走在正确的轨道上,但你需要更进一步,再为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文件将生成不同的哈希名称。)

评论中的网址可以向MSDN解释一下。

相关问题