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.

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

Here’s how to use it.

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