V2: Caching assets - how?

1. My Caddy version (caddy -version):

v2.0.0-beta.13 h1:QL0JAepFvLVtOatABqniuDRQ4HmtvWuuSWZW24qVVtk=

3. The problem I’m having:

I want to make sure static resources are cached in users’ browsers for a year or more, as recommended by Google: Serve static assets with an efficient cache policy

I used to accomplish this, I believe correctly, by using https://caddyserver.com/v1/docs/http.expires in Caddy V1.

How do I do this in Caddy V2? Or is it not implemented yet?

Welcome around, Nelson! You can use the headers handler to set the value of Cache-Control headers. So you should have something like this in your array of handle:

{
	"handler": "headers",
	"response": {
		"set": {
			"Cache-Control": [
				"max-age=31536000"
			]
		}
	}
}

And depending on your directory structure, you’d use either the path or path_regexp matchers.

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.