webzod
account

Make the largest element on the page load faster on Caddy

perf.cwv.lcp_poor · effort: about an hour

Why it matters

Largest Contentful Paint is how long visitors wait before the main image or heading shows, and a poor value means many of them wait too long. Google counts this in its page experience signals and slow pages lose visitors.

Staged change. Do this in stages and check each one before the next, so a mistake does not break your site or your mail.

The fix

  1. Turn on compression and give files whose names change with their content a long cache lifetime. Use the long lifetime only for fingerprinted files such as app.3f9c2a.js; for others use a shorter max-age.
  2. The long lifetime is set before the file is looked up, so the handle_errors block in the snippet removes it from error pages such as a missing file. It covers files that Caddy serves itself; for files from an application behind reverse_proxy, set Cache-Control in the application.
  3. Run caddy reload and check the Cache-Control and Content-Encoding headers with curl -I.
Caddyfile
example.com {
	encode zstd gzip
	@static path_regexp static \.(css|js|woff2|avif|webp|png|jpe?g|svg)$
	header @static Cache-Control "public, max-age=31536000, immutable"
	root * /var/www/html
	file_server
	handle_errors {
		header -Cache-Control
		respond "{err.status_code} {err.status_text}"
	}
	# keep the rest of your site configuration here
}

The examples use example.com. Replace it with your own domain.

Read the documentation (opens in a new tab)

Scan your domain to see whether this applies to you.