Why this matters
Accurate MIME types and tailored caching improve compatibility and speed.
For PHP-served assets, emit the precise Content-Type (e.g., text/css; charset=utf-8, application/javascript, font/woff2) and set immutable caching for hashed files; never apply immutable caching to HTML.
Accurate MIME types and tailored caching improve compatibility and speed.
Side-by-side examples engineers can pattern-match during review.
<?php header("Content-Type: text/plain"); readfile("app.js");<?php if (preg_match('/\\.[0-9a-f]{8,}\\./', \$file)) { header('Cache-Control: public, max-age=31536000, immutable'); } header('Content-Type: application/javascript; charset=utf-8'); readfile(\$file);header("Cache-Control: public, max-age=31536000, immutable");header("Content-Type: text/plain");From the same buckets as this rule.
All static JS/CSS/font/image files MUST use content-hashed filenames (e.g., app.9c1a7b.js) and be served with "Cache-Control: public, max-age=31536000, immutable". HTML and other non-fingerprinted documents MUST be served with "Cache-Control: no-cache" (or equivalent) to enable conditional revalidation.
Serve text-based assets (JS, CSS, JSON, SVG) with Brotli (br) when the client sends "Accept-Encoding: br" and fallback to gzip. Always set "Vary: Accept-Encoding" and do NOT compress already-compressed formats (e.g., .png, .jpg, .woff2).