简体   繁体   中英

apache cache-control without validation

how can I make my browser cache an asset such as an image with a command to tell the browser not to check for updated files? I have static assets that won't ever change and it's a waste to keep asking the server for updates.

My host doesn't declare favicon.ico files as image/x-icon, so I need the following

AddType image/x-icon .ico

You set expires as follows, one per type. Here is an example set (you can use day, week, month, etc. :

ExpiresActive On
ExpiresByType text/css        "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType image/gif       "access plus 1 week"
ExpiresByType image/png       "access plus 1 week"
ExpiresByType image/x-icon    "access plus 1 week"

You might also wish to set the Cache header

<FilesMatch "\.(css|js|gif|jpe?g|png|ico)$">
   Header add "Cache-Control" "public, max-age=604800"
</FilesMatch>

Extend as required for other types. 604800 = 1 week in seconds. Do the math if you want to change this.

How the browser uses these headers

RFC 2616 (HTTP/1.1) lays down a framework advice of when and how browsers use this information. To get an insight into how this works in practice use the Google Chrome developer view (Ctl+Shft+I) Network tab. (Most other major browsers offer equivalent instrumentation though possibly through an extension pack.) However this is advisory in that users can elect to ignore this though their browser options and always elect to check the server, or to check the server once per session. IIRC, some early browsers (eg MSIE pre 8) had the check-once-per session as the default option.

Also if you elect to do a page refresh, then most browsers will revalidate all resources on the page using the If-Modified-Since and If-None-Match , so you need to revisit a link or re-enter the URI to see the normal reuse action. Likewise most will honour Shift+refresh as a "forced refresh" -- that is no negotiation is carried out.

When I administered the OpenOffice.orf user forums, I once analysed a full month's access logs to see how much caching was being carried out by using a simple algo to determine when reused resources should be cached but where still being fetched by client browsers. I was surprised to find that this cache-miss rate was over 30% (due to users' browser settings). However most users access these user forums as a knowledgebase using google to ask about specific problem and then look at a few pages, never to return within the month. When I restricted myself to the Moderator's IPs (who mostly use Ff or Chrome as their browser) then this rate increased to ~95%.

So clients browsers do use this information, but not as much as you might expect. I also discuss here that browsers also can cache resources when you don't explicitly request this.

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