简体   繁体   English

浏览器缓存中的静态内容

[英]Browser cache for static contents

I am trying to update the configuration which effects the browser caching settings for static resources (js, css, images). 我正在尝试更新配置,该配置会影响静态资源(js,css,图像)的浏览器缓存设置。

I have Sitecore CMS site and images uploaded in CMS, these images are cached in browser (status code = 200 (from cache), when i observed the network in Chrome browser), but my other resources like js and css which served from Website folder are not cached in browser, and the status code is 304 (which is server cache and there is a round trip required to check for any update.) 我有Sitecore CMS网站和在CMS中上传的图像,这些图像被缓存在浏览器中(当我在Chrome浏览器中观察到网络时,状态代码= 200(来自缓存)),但是我的其他资源(例如js和CSS)从“网站”文件夹中投放没有缓存在浏览器中,并且状态码是304(这是服务器缓存,并且需要往返来检查任何更新。)

I have below config settings in web.config file: 我在web.config文件中有以下配置设置:


    <caching>
        <profiles>
            <add extension=".jpeg" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
            <add extension=".js" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
            <add extension=".png" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
            <add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
            <add extension=".css" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
            <add extension=".json" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="00:00:30" />
        </profiles>
    </caching>
    <staticContent>
        <remove fileExtension=".woff2" />
        <mimeMap fileExtension=".json" mimeType="application/json" />
        <mimeMap fileExtension=".woff2" mimeType="application/x-font-woff2" />
        <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
    </staticContent>

Now if i remove the caching-profiles entry, then the resources are cached in browser, i can see all the css, js status code as 200 (from cache) for the subsequent request. 现在,如果删除缓存配置文件条目,那么资源将缓存在浏览器中,对于后续请求,我可以看到所有css,js状态代码为200(来自缓存)。

My question here is, what is the difference here, why browser cache didn't worked even there is a staticContent settings are added. 我的问题是,这里有什么区别,即使添加了staticContent设置,为什么浏览器缓存也无法工作。 What is the impact if I remove the existing caching-profiles settings (will it impacts server cache?). 如果删除现有的缓存配置文件设置会产生什么影响(会影响服务器缓存吗?)。

I saw some other links , which explains static cache settings, but i want to know the impact with the changes i did (removed caching-profiles) 我看到了其他一些链接 ,这些链接解释了静态缓存设置,但是我想知道所做的更改所带来的影响(删除了缓存配置文件)

Please let me know your inputs. 请让我知道您的输入。

Below are the Response header details: With caching-profiles : 以下是Response标头的详细信息:With caching-profiles

在此处输入图片说明

Without caching-profiles : 没有缓存配置文件

在此处输入图片说明 Thanks, Sharath 谢谢,Sharath

So, it appears you are configuring IIS output cache for .jpeg , .js , ..., within the web.config system.webServer/caching node (beware, .webServer , not .web ). 因此,看来您是在web.config system.webServer/caching节点(请注意, .webServer而不是.web )内为.jpeg.js ,...配置IIS输出缓存

Unfortunately, IIS output cache (and Asp.Net output cache too by the way) does also handle client caching, and so it interferes (badly) with your client caching settings. 不幸的是,IIS输出缓存(顺便说一下,还有Asp.Net输出缓存 )也可以处理客户端缓存,因此(严重)干扰了客户端缓存设置。 (It should be two separate matters in my opinion, but that is not the way IIS/Asp.Net output caches handle it.) (我认为这应该是两个单独的问题,但这不是IIS / Asp.Net输出缓存处理它的方式。)

Your profiles do not set the location attribute , so they default to Server . 您的配置文件未设置location属性 ,因此它们默认为Server With output cache semantic, this means "no client cache", thus the no-cache it adds in Cache-Control response header. 对于输出缓存语义,这意味着“无客户端缓存”,因此它在Cache-Control响应头中添加了no-cache

To avoid this, you may change your profile location to Any . 为避免这种情况,您可以将个人资料location更改为Any

<profiles>
  <add extension=".jpeg" location="Any" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
  <add extension=".js" location="Any" policy="CacheUntilChange" kernelCachePolicy="DontCache"  />
  <add extension=".png" location="Any" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
  <add extension=".jpg" location="Any" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
  <add extension=".css" location="Any" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
  <add extension=".json" location="Any" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="00:00:30" />
</profiles>

But maybe should you simply not use IIS output-cache for .jpeg , .js , ... 但也许您不应该将IIS输出缓存不用于.jpeg.js ,...
From official IIS documentation : 正式的IIS文档中

Output caching is unnecessary for static files, such as HTML, JPG, or GIF files, ... 对于静态文件(例如HTML,JPG或GIF文件),不需要输出缓存。

It has no benefit using IIS output cache for static files. 将IIS输出缓存用于静态文件没有任何好处。 (It may have benefits using an actual cache server such as varnish in front of your IIS, but emulating a cache server with IIS output cache has no benefit for static files.) (在IIS之前使用实际的缓存服务器(例如varnish)可能会有好处,但是使用IIS输出缓存模拟缓存服务器对静态文件没有好处。)

If you have some special cases URIs endings with static file extension but actually served dynamically by your application instead of directly corresponding to a file on disk (special case usually involving using rammfar which is a bad thing for application scalability; linked page gives some alternates by the way), better try enabling output-cache only for those URIs, by configuring it under 如果您有一些特殊情况,则URI以静态文件扩展名结尾,但实际上是由您的应用程序动态提供的,而不是直接与磁盘上的文件相对应的(特殊情况下,通常涉及使用rammfar ,这对应用程序的可伸缩性是不利的;链接页会通过的方式),最好尝试通过仅在以下URI中启用输出缓存来进行配置

<configuration>
  ...
  <system.webServer>
    ... <!-- not here! -->
  </system.webServer>
  ...
  <location path="yourDynamicImagesUriBasePath">
    <system.webServer>
      <caching>
        ... <!-- move it here -->

(I am not sure it works though, better test it of course.) (不过,我不确定它是否可以正常工作,请更好地对其进行测试。)

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

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