简体   繁体   中英

How to let the browser cache css and js files for a https site?

I develop an ASP.NET web application hosted in IIS 8.5 using https. I use fiddler to track the request and response, and find that css and js files are not cached by the browser (IE 11). For each page of the website, these files will be downloaded again. Here are the request and response. My question is why the reponse says "Cache-Control: no-cache", do I need to set anything in IIS?

GET https://***.com/Scripts/jquery-ui-1.8.20.js HTTP/1.1
Accept: application/javascript, */*;q=0.8
Referer: https://***.com/
Accept-Language: en-US
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: ***.com
Connection: Keep-Alive
Cookie: __AntiXsrfToken=000b8dfbde4b40a68a19a2ee3fc1195a; ASP.NET_SessionId=qdhotusxrcchnaowhagzol1y


HTTP/1.1 200 OK
Cache-Control: no-cache
Transfer-Encoding: chunked
Content-Type: application/javascript
Content-Encoding: gzip
Last-Modified: Tue, 16 Jul 2013 09:41:03 GMT
Accept-Ranges: bytes
ETag: "e74a9b96882ce1:0"
Vary: Accept-Encoding
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
X-UA-Compatible: IE=edge
Date: Fri, 24 Jan 2014 02:28:24 GMT

据我了解, Cache-Control: no-cache并不意味着浏览器没有完全缓存,而是告诉浏览器缓存内容,浏览器应使用Etag或Date标头查询缓存内容是否有任何变化。

Set the cache for static files either in web.config or via UI in IIS, for the static file folder you want to cache, add this in the config file and it works.

<configuration>
    <system.webServer>
        <staticContent>
            <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1000.00:00:00" />
        </staticContent>
        <httpProtocol>
            <customHeaders>
                <add name="Cache-Control" value="public" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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