Clarification on usage of “Cache-Control” header

2019-05-28 09:08发布

We are trying to improve the page load time of a site. In addition to the few other optimizations, I have added a servlet filter to set "Cache-Control" headers to image files(png,gif,jpg). As expected,the image files are now getting cached at the client end and I could verify the expires time of each file by inspecting the temp folder(IE).My question is, browsers still request the static resources and gets a HTTP304 (Not modified) response. I mean,in effect what i have achieved is I have reduced the no of bytes transferred but not the number of network roundtrips (Which in my case is little slow). Is there any other header/way to stop browsers checking the server whether the resource has been modified?

3条回答
Explosion°爆炸
2楼-- · 2019-05-28 09:48

@chedine,

Browser cached files should not return a 304 they should return a 200. You should use mod_expires to set the max age and mod_headers to set the Cache-Control headers. Here is an example that returns a valid 200 response for browser cached static files:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css M5184000
    ExpiresByType application/x-javascript M5184000
    ExpiresByType text/html M1200
    ExpiresByType text/plain M1200
    ExpiresByType image/gif M31536000
    ExpiresByType application/x-gzip M31536000
    ExpiresByType image/x-icon M31536000
    ExpiresByType image/jpeg M31536000
    ExpiresByType image/png M31536000
</IfModule>

<FilesMatch "\.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$">
    <IfModule mod_headers.c>
         Header set Vary "Accept-Encoding, Cookie"
        Header set Pragma "public"
        Header append Cache-Control "public, must-revalidate, proxy-revalidate"
    </IfModule>
</FilesMatch>
查看更多
一夜七次
3楼-- · 2019-05-28 09:55

What are you cache-control settings, you should be able to set max-age and have it not come back for a period of time.

You might want to try fiddler to look at your http request/response and make sure it is what you think it is.

see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html for details

查看更多
干净又极端
4楼-- · 2019-05-28 10:03

Yes, on the browser itself. e.g. in IE, under Browsing History settings, option "Check for newer versions of stored pages" - selecting "Never" will then 'honour' the expiry and will avoid the request / not modified trip to the server, until the stated expiry date arrives.

查看更多
登录 后发表回答