Skip to content

Add Cache-Control snippet for static assets to frankenphp images - #4

Merged
michaelaguiar merged 1 commit into
mainfrom
static-asset-cache-headers
Sep 10, 2026
Merged

michaelaguiar merged 1 commit into
mainfrom
static-asset-cache-headers

Conversation

@michaelaguiar

Copy link
Copy Markdown
Member

Summary

  • docker-configure renders a second Caddy snippet, 10-cache-headers.caddyfile, from CADDY_IMMUTABLE_ASSET_PATHS (content-hashed / ?ver= assets, public, max-age=31536000, immutable) and CADDY_MEDIA_PATHS (uploads, public, max-age=2592000). Nothing is emitted unless a list is set, so the base and dev images behave exactly as before.
  • Dockerfile.wordpress sets defaults covering both Bedrock and stock layouts, cache-plugin output, and Avada's compiled fusion-styles / fusion-scripts dirs.
  • Applied to 8.2, 8.3, 8.4 and 8.5 (boot scripts were already byte-identical across versions).
  • README: new "Static asset caching" section with a Laravel example, and a pointer from the WordPress hardening section.

Why

Caddy's file_server sends no Cache-Control. Browsers revalidate every asset on every visit, and Cloudflare falls back to a short edge TTL and revalidates from every PoP independently. On the TMG network this was 42% of origin requests (about 30k 304s per day on thehairsociety.org alone, from 6,300 distinct Cloudflare edge IPs).

Design notes

  • The file matcher inside each named matcher limits the header to files that exist on disk, so a 404 never carries a long TTL.
  • * in a prefix matches a single path segment (multisite uploads/sites/*/). Dots and + are escaped.
  • HTML, PHP and JSON responses are never matched. WP-Optimize's Cache-Control: no-cache on pages is untouched.
  • Media gets 30 days rather than a year because a media-library file replaced in place keeps its filename.
  • A stale snippet from a previous boot is removed if both lists are cleared.

Test plan

  • Ran the modified script inside a live 8.4-frankenphp-wordpress container with output redirected to a temp dir; rendered snippet matched expectations.
  • Served the rendered hardening + cache snippets together on a throwaway Caddy listener in that container: jQuery, ?ver= plugin CSS, wpo-minify bundles and Avada compiles get the immutable header; a media image gets 30 days; JSON under uploads, wp-cron.php, missing files, a PHP file under uploads and readme.html are untouched (the latter two still 404 from the hardening rules).
  • Same script with no CADDY_* lists set: only 00-hardening.caddyfile is written.
  • sh -n on all four docker-configure copies; frankenphp validate on the rendered config.
  • Build and push, recreate the TMG containers, confirm cache-control and cf-cache-status from outside through Cloudflare and watch the origin 304 rate drop.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TW7f1vNou3AhkWSbhwPQuY

Caddy's file_server sends no Cache-Control, so browsers revalidate every
asset on every visit and Cloudflare falls back to a short edge TTL and
revalidates from every PoP. On the TMG WordPress network that was 42% of
origin requests (30k 304s/day on one site).

docker-configure now renders 10-cache-headers.caddyfile at boot from two
path lists: CADDY_IMMUTABLE_ASSET_PATHS (content-hashed / ?ver= assets,
1 year immutable) and CADDY_MEDIA_PATHS (uploads, 30 days). Nothing is
emitted unless a list is set, so the base and dev images are unchanged.
The `file` matcher limits the header to files that exist, so 404s are
never cached long-term. "*" in a prefix matches one path segment.

Dockerfile.wordpress sets defaults for both the Bedrock and stock layouts,
including cache-plugin output and Avada's compiled fusion-styles /
fusion-scripts directories. Applied to 8.2-8.5; the boot scripts are
identical across versions.

Verified by rendering the snippet inside a live container and serving it
with the hardening snippet on a throwaway listener: versioned assets get
the immutable header, media gets 30 days, JSON/PHP/HTML and 404s are
untouched, and the no-exec upload handlers still apply.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TW7f1vNou3AhkWSbhwPQuY
@michaelaguiar
michaelaguiar merged commit e35ed5e into main Sep 10, 2026
2 checks passed
@michaelaguiar
michaelaguiar deleted the static-asset-cache-headers branch September 11, 2026 00:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant