代码之家  ›  专栏  ›  技术社区  ›  Chuck Le Butt

要求浏览器缓存我们的图像(ASP.NET/IIS)

  •  19
  • Chuck Le Butt  · 技术社区  · 16 年前

    我只是跑 Google's Page Speed 针对我们网站的申请,其中一项建议是 浏览器缓存 . 扩展后发现:

    以下可缓存资源具有 短暂的新鲜寿命: 在中指定至少一周的过期时间 以下资源的未来:

    < 一长串图像 >
    < 一些javascript文件 >

    如何延长特定图像的“新鲜寿命”?

    这是一个 ASP.NET 正在运行的项目 IIS7.5

    3 回复  |  直到 10 年前
        1
  •  49
  •   Community Mohan Dere    9 年前

    我在这个网站的其他地方找到了我的问题的答案。呜呜!(不知道为什么我第一次发布这个时它没有出现,但没关系,我最终到了那里。)

    对于那些感兴趣的人,答案是 Gabriel McAdams ):


    你在IIS中这样做。如果使用的是IIS 7,则可以在web.config中添加头。它位于system.webserver部分。

    <staticContent>
        <clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />
    </staticContent>
    

    这将导致所有静态内容的ExpiresHTTP头设置为2020年。静态内容是指通过ASP.NET引擎未提供的任何内容,如图像、脚本文件和样式表。

    或者使用相对过期时间,请使用:

    <staticContent>
        <clientCache cacheControlMaxAge ="2.00:00:00" cacheControlMode="UseMaxAge" />
    </staticContent>
    

    这将导致所有静态内容的过期HTTP头设置为2天。

        2
  •  4
  •   H. Pauwelyn    10 年前

    您必须将Expires头添加到静态内容中,包括图像、HTML、JS、CSS文件。您可以轻松地在web.config_s中添加Expires头文件。 system.webServer 使用IIS7的截面:

    <staticContent>
        <clientCache httpExpires="Mon, 1 May 2020 05:00:00 GMT" cacheControlMode="UseExpires" />
    </staticContent>
    
        3
  •  2
  •   Miss.Vy    10 年前

    您通常希望缓存所有资产(CSS、JS和图像),HTML文件将 çache busting links 以便您仍然可以更新图像。

    为了使其工作,HTML文件不能是 staticContent . 为HTML文件添加新的处理程序可防止它们被永久缓存。

        <staticContent>
            <clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />
        </staticContent>
        <handlers>
          <add
            name="HtmlHandler"
            path="*.html"
            verb="*"
            type="System.Web.Handlers"
            preCondition="integratedMode"
          />
        </handlers>