Skip to content

Commit

Permalink
Cache-Control boilerplate with extensive control (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoColomb authored Dec 5, 2022
1 parent dbc38dd commit f650cb5
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/htaccess.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
run: ./bin/build.sh test/conf/.htaccess test/build/htaccess_fixture.conf

- name: Test with server-configs-test
uses: h5bp/server-configs-test@6.2.0
uses: h5bp/server-configs-test@7.0.0
with:
command: test
server: httpd
Expand All @@ -29,7 +29,7 @@ jobs:
tests: basic-file-access:cache-busting:custom-errors:forbidden-files:enforce-gzip:precompressed-files-gzip:concatenation

- name: Benchmark
uses: h5bp/server-configs-test@6.2.0
uses: h5bp/server-configs-test@7.0.0
with:
command: benchmark
server: httpd
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
"httpd.conf"
- name: Test with server-configs-test
uses: h5bp/server-configs-test@6.2.0
uses: h5bp/server-configs-test@7.0.0
with:
command: test
server: httpd
Expand All @@ -41,7 +41,7 @@ jobs:
tests: basic-file-access;caching;cache-busting;custom-errors;forbidden-files;enforce-gzip;precompressed-files-gzip;rewrites;ssl

- name: Benchmark
uses: h5bp/server-configs-test@6.2.0
uses: h5bp/server-configs-test@7.0.0
with:
command: benchmark
server: httpd
Expand Down
3 changes: 2 additions & 1 deletion bin/htaccess.conf
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ title "web performance"
enable "h5bp/web_performance/compression.conf"
disable "h5bp/web_performance/pre-compressed_content_brotli.conf"
disable "h5bp/web_performance/pre-compressed_content_gzip.conf"
disable "h5bp/web_performance/content_transformation.conf"
enable "h5bp/web_performance/no_etags.conf"
enable "h5bp/web_performance/cache_expiration.conf"
disable "h5bp/web_performance/cache-control.conf"
disable "h5bp/web_performance/content_transformation.conf"
disable "h5bp/web_performance/file_concatenation.conf"
disable "h5bp/web_performance/filename-based_cache_busting.conf"
65 changes: 65 additions & 0 deletions h5bp/web_performance/cache-control.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# ----------------------------------------------------------------------
# | Cache Control |
# ----------------------------------------------------------------------

# Serve resources with appropriate cache control directives.
#
# The `Cache-Control` header field holds directives (instructions) that control
# caching in browsers and shared caches (e.g. Proxies, CDNs).
# Its use targets web performances improvement by specifying the expected
# client and network caches behaviors.
#
# The usable cache directives are listed here:
# https://www.iana.org/assignments/http-cache-directives/http-cache-directives.xml
#
# The cache directives are documented here:
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#response_directives
#
# (!) Enable and configure this configuration with care.
# Default values should embrace conformance for static files and simple
# apps, but cache control definition at backend level is highly preferred.
# Incorrect directives can lead to data leaks, or can degrade performances.
#
# More specifically, in-depth understanding on `public` vs `private`
# directives meanings is highly recommended. A resource with `public` will
# be cached by shared caches like CDN, even if a user session is active.
#
# (!) The config directive `Header` must be used with the appropriate action.
# Depending on the need, `merge` keeps the current value, if any, of
# `Cache-Control` header, while `set` reset the value including the one
# added by `ExpiresByType` directive in the cache expiration config file
# h5bp/web_performance/cache_expiration.conf.
# https://httpd.apache.org/docs/current/mod/mod_headers.html#header
#
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
# https://www.rfc-editor.org/rfc/rfc9111.html
# https://www.rfc-editor.org/rfc/rfc8246.html
# https://www.rfc-editor.org/rfc/rfc5861.html
# https://www.iana.org/assignments/http-cache-directives/http-cache-directives.xml
# https://cache-tests.fyi/

<IfModule mod_headers.c>

# Default
Header merge Cache-Control "public, immutable, stale-while-revalidate" "expr=%{resp:Cache-Control} == 'max-age=31536000'"

# No content
Header merge Cache-Control "no-store" "expr=-z %{CONTENT_TYPE}"

# Manifest files
Header merge Cache-Control "public" "expr=%{CONTENT_TYPE} =~ m#application/manifest\+json#i"
Header set Cache-Control "no-cache" "expr=%{CONTENT_TYPE} =~ m#text/cache-manifest#i"

# Assets
Header merge Cache-Control "public, immutable, stale-while-revalidate" "expr=%{CONTENT_TYPE} =~ m#image/x-icon#i"

# Data interchange
Header merge Cache-Control "public, stale-while-revalidate" "expr=%{CONTENT_TYPE} =~ m#application/(atom|rdf|rss)\+xml#i"

# Documents
Header set Cache-Control "no-cache, private, must-revalidate" "expr=%{CONTENT_TYPE} =~ m#text/(html|markdown|calendar)#i"

# Data
Header set Cache-Control "no-cache" "expr=%{CONTENT_TYPE} =~ m#json|xml#i && %{CONTENT_TYPE} !~ m#/(atom|rdf|rss|manifest|svg)\+#i"

</IfModule>
3 changes: 2 additions & 1 deletion test/build/htaccess_fixture.conf
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ title "web performance"
enable "h5bp/web_performance/compression.conf"
enable "h5bp/web_performance/pre-compressed_content_brotli.conf"
enable "h5bp/web_performance/pre-compressed_content_gzip.conf"
omit "h5bp/web_performance/content_transformation.conf"
enable "h5bp/web_performance/no_etags.conf"
enable "h5bp/web_performance/cache_expiration.conf"
enable "h5bp/web_performance/cache-control.conf"
omit "h5bp/web_performance/content_transformation.conf"
enable "h5bp/web_performance/file_concatenation.conf"
enable "h5bp/web_performance/filename-based_cache_busting.conf"
1 change: 1 addition & 0 deletions test/vhosts/server.localhost.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Include h5bp/security/content-security-policy.conf
Include h5bp/security/permissions-policy.conf
Include h5bp/security/cross-origin-policy.conf
Include h5bp/web_performance/cache-control.conf
Include h5bp/web_performance/filename-based_cache_busting.conf
Include h5bp/errors/custom_errors.conf

Expand Down

0 comments on commit f650cb5

Please sign in to comment.