Enable caching of css and js files in Apache

后端 未结 1 729
梦谈多话
梦谈多话 2021-02-13 10:14

Using Apache 2.4 on Debian 8.2, I am trying to enable caching of all css and js files. Caching of images works fine; that is, the browser receives a 304 status, so it doesn\'t d

相关标签:
1条回答
  • 2021-02-13 10:38

    Turn off Etags, they don't play well in Apache when gzip is on for 304s.

    See here: Apache is not sending 304 response (if mod_deflate and AddOutputFilterByType is enabled)

    As images are already compressed they are typically not gzipped and hence why they work.

    ETags are not that useful in my opinion in their current implementation (see my blog here for a more in depth discussion as to why) so, coupled with above bug, I turn them off.

    For your second question just set a long expiry.

    As discussed in the comments below there are three scenarios:

    1. Normal browsing - in which caching should be used and 304s only used if cache is still valid after expiry (in which case it's set to valid again for same expiry).

    2. F5 or Refresh button. This is an explicit action by the user to confirm the page and all its resources are still valid so they all will be double checked (even those still in cache and still valid according to expiries header) and 304s sent when they haven't changed. It does not mean "just redownload anything which has expired but leave cached items alone as they are still valid" as you think it should. Personally I think the current implementation the browsers use makes sense and your method would be confusing to end users. While some sites may version assets like images, css and JavaScript so rechecking is a waste of time, not all such sites do this.

    3. Ctrl+F5. This is a force refresh. It means "Ignore cache and download everything". It's rarely needed except by developers who change files requested on development servers.

    Hope that all makes sense.

    Edit 12 May 2016: Looks like Firefox is bringing in the functionality you actually want: https://bitsup.blogspot.ie/2016/05/cache-control-immutable.html?m=1

    0 讨论(0)
提交回复
热议问题