|
| 1 | +# ---------------------------------------------------------------------- |
| 2 | +# | Cache Control | |
| 3 | +# ---------------------------------------------------------------------- |
| 4 | + |
| 5 | +# Serve resources with appropriate cache control directives. |
| 6 | +# |
| 7 | +# The `Cache-Control` header field holds directives (instructions) that control |
| 8 | +# caching in browsers and shared caches (e.g. Proxies, CDNs). |
| 9 | +# Its use targets web performances improvement by specifying the expected |
| 10 | +# client and network caches behaviors. |
| 11 | +# |
| 12 | +# The usable cache directives are listed here: |
| 13 | +# https://www.iana.org/assignments/http-cache-directives/http-cache-directives.xml |
| 14 | +# |
| 15 | +# The cache directives are documented here: |
| 16 | +# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#response_directives |
| 17 | +# |
| 18 | +# (!) Enable and configure this configuration with care. |
| 19 | +# Default values should embrace conformance for static files and simple |
| 20 | +# apps, but cache control definition at backend level is highly preferred. |
| 21 | +# Incorrect directives can lead to data leaks, or can degrade performances. |
| 22 | +# |
| 23 | +# More specifically, in-depth understanding on `public` vs `private` |
| 24 | +# directives meanings is highly recommended. A resource with `public` will |
| 25 | +# be cached by shared caches like CDN, even if a user session is active. |
| 26 | +# |
| 27 | +# (!) The config directive `Header` must be used with the appropriate action. |
| 28 | +# Depending on the need, `merge` keeps the current value, if any, of |
| 29 | +# `Cache-Control` header, while `set` reset the value including the one |
| 30 | +# added by `ExpiresByType` directive in the cache expiration config file |
| 31 | +# h5bp/web_performance/cache_expiration.conf. |
| 32 | +# https://httpd.apache.org/docs/current/mod/mod_headers.html#header |
| 33 | +# |
| 34 | +# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control |
| 35 | +# https://www.rfc-editor.org/rfc/rfc9111.html |
| 36 | +# https://www.rfc-editor.org/rfc/rfc8246.html |
| 37 | +# https://www.rfc-editor.org/rfc/rfc5861.html |
| 38 | +# https://www.iana.org/assignments/http-cache-directives/http-cache-directives.xml |
| 39 | +# https://cache-tests.fyi/ |
| 40 | + |
| 41 | +<IfModule mod_headers.c> |
| 42 | + |
| 43 | + # Default |
| 44 | + Header merge Cache-Control "public, immutable, stale-while-revalidate" "expr=%{resp:Cache-Control} == 'max-age=31536000'" |
| 45 | + |
| 46 | + # No content |
| 47 | + Header merge Cache-Control "no-store" "expr=-z %{CONTENT_TYPE}" |
| 48 | + |
| 49 | + # Manifest files |
| 50 | + Header merge Cache-Control "public" "expr=%{CONTENT_TYPE} =~ m#application/manifest\+json#i" |
| 51 | + Header set Cache-Control "no-cache" "expr=%{CONTENT_TYPE} =~ m#text/cache-manifest#i" |
| 52 | + |
| 53 | + # Assets |
| 54 | + Header merge Cache-Control "public, immutable, stale-while-revalidate" "expr=%{CONTENT_TYPE} =~ m#image/x-icon#i" |
| 55 | + |
| 56 | + # Data interchange |
| 57 | + Header merge Cache-Control "public, stale-while-revalidate" "expr=%{CONTENT_TYPE} =~ m#application/(atom|rdf|rss)\+xml#i" |
| 58 | + |
| 59 | + # Documents |
| 60 | + Header set Cache-Control "no-cache, private, must-revalidate" "expr=%{CONTENT_TYPE} =~ m#text/(html|markdown|calendar)#i" |
| 61 | + |
| 62 | + # Data |
| 63 | + Header set Cache-Control "no-cache" "expr=%{CONTENT_TYPE} =~ m#json|xml#i && %{CONTENT_TYPE} !~ m#/(atom|rdf|rss|manifest|svg)\+#i" |
| 64 | + |
| 65 | +</IfModule> |
0 commit comments