Caddy Caching for Static Sites

This is how I am currently setting up my Caddyfor for caching static sites. I assume that files like index.html are going to get built and rebuilt and really shouldn’t be cached. But files like style.css might never change and should be hashed to a filename like style.7de89dba35df2c5e5239.css. So we can cache files like that forever. Make sure this approach is right for you before you use it. Feedback is welcomed.

Here’s the snippet.

(cache) {
	@assets {
		path *.bmp *.jpg *.png *.svg *.gif *.pdf *.css *.js
	}
	@content {
		not @assets
	}
	header @assets cache-control max-age=31536000
	header @content cache-control max-age=0
}

Here’s how to use it.

mysite.com {
	root * /srv/
	file_server
	import cache
}