Skip to content

Commit f650cb5

Browse files
authored
Cache-Control boilerplate with extensive control (#325)
1 parent dbc38dd commit f650cb5

File tree

6 files changed

+74
-6
lines changed

6 files changed

+74
-6
lines changed

.github/workflows/htaccess.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
run: ./bin/build.sh test/conf/.htaccess test/build/htaccess_fixture.conf
2121

2222
- name: Test with server-configs-test
23-
uses: h5bp/server-configs-test@6.2.0
23+
uses: h5bp/server-configs-test@7.0.0
2424
with:
2525
command: test
2626
server: httpd
@@ -29,7 +29,7 @@ jobs:
2929
tests: basic-file-access:cache-busting:custom-errors:forbidden-files:enforce-gzip:precompressed-files-gzip:concatenation
3030

3131
- name: Benchmark
32-
uses: h5bp/server-configs-test@6.2.0
32+
uses: h5bp/server-configs-test@7.0.0
3333
with:
3434
command: benchmark
3535
server: httpd

.github/workflows/server.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
"httpd.conf"
3232
3333
- name: Test with server-configs-test
34-
uses: h5bp/server-configs-test@6.2.0
34+
uses: h5bp/server-configs-test@7.0.0
3535
with:
3636
command: test
3737
server: httpd
@@ -41,7 +41,7 @@ jobs:
4141
tests: basic-file-access;caching;cache-busting;custom-errors;forbidden-files;enforce-gzip;precompressed-files-gzip;rewrites;ssl
4242

4343
- name: Benchmark
44-
uses: h5bp/server-configs-test@6.2.0
44+
uses: h5bp/server-configs-test@7.0.0
4545
with:
4646
command: benchmark
4747
server: httpd

bin/htaccess.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ title "web performance"
8181
enable "h5bp/web_performance/compression.conf"
8282
disable "h5bp/web_performance/pre-compressed_content_brotli.conf"
8383
disable "h5bp/web_performance/pre-compressed_content_gzip.conf"
84-
disable "h5bp/web_performance/content_transformation.conf"
8584
enable "h5bp/web_performance/no_etags.conf"
8685
enable "h5bp/web_performance/cache_expiration.conf"
86+
disable "h5bp/web_performance/cache-control.conf"
87+
disable "h5bp/web_performance/content_transformation.conf"
8788
disable "h5bp/web_performance/file_concatenation.conf"
8889
disable "h5bp/web_performance/filename-based_cache_busting.conf"
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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>

test/build/htaccess_fixture.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ title "web performance"
7979
enable "h5bp/web_performance/compression.conf"
8080
enable "h5bp/web_performance/pre-compressed_content_brotli.conf"
8181
enable "h5bp/web_performance/pre-compressed_content_gzip.conf"
82-
omit "h5bp/web_performance/content_transformation.conf"
8382
enable "h5bp/web_performance/no_etags.conf"
8483
enable "h5bp/web_performance/cache_expiration.conf"
84+
enable "h5bp/web_performance/cache-control.conf"
85+
omit "h5bp/web_performance/content_transformation.conf"
8586
enable "h5bp/web_performance/file_concatenation.conf"
8687
enable "h5bp/web_performance/filename-based_cache_busting.conf"

test/vhosts/server.localhost.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Include h5bp/security/content-security-policy.conf
1313
Include h5bp/security/permissions-policy.conf
1414
Include h5bp/security/cross-origin-policy.conf
15+
Include h5bp/web_performance/cache-control.conf
1516
Include h5bp/web_performance/filename-based_cache_busting.conf
1617
Include h5bp/errors/custom_errors.conf
1718

0 commit comments

Comments
 (0)