Custom cache headers on file_server index pages in v2

1. Caddy version (caddy version):

v2.0.0

2. How I run Caddy:

I am trying to put together configuration for Caddy that caches assets like JS and CSS for a long time but causes the HTML pages to always re-validate. The following configuration is doing the trick currently, with the @html matcher using path matching to match index pages, but I’m wondering if there is a better way to achieve this.

file_server {
    root "{$DATA}/www"
}

@assets {
    path "*.js"
    path "*.css"
}
header @assets {
    Cache-Control "max-age=31536000"
}

@html {
    path */
    not path /api*
}
header @html {
    Cache-Control "must-revalidate"
}

a. System environment:

Caddy binary on macOS

b. Command:

caddy run --config Caddyfile

c. Service/unit/compose file:

N/A

d. My complete Caddyfile or JSON config:

Can provide more detail if needed

3. The problem I’m having:

I’m wondering if there is a better way to implement the match for HTML pages.

My thinking has been that I’d like to do this by looking at a matcher on the response Content-Type header for text/html* that I can then modify things for, after the file_server module has picked the file and served it.

5. What I already tried:

  • Specifying header Content-Type text/html* in the @html matcher, but this is just for the request headers and doesn’t work
  • Specifying path *.html in the @html matcher, in the hope that paths were internally re-written to be the full index.html page, but this doesn’t work

6. Links to relevant resources:

It should be max-age=

Thanks, that was a typo that I think should be unrelated to what I’m hoping to achieve for the file_server index pages.

I updated the post to reflect the correct value.

That is also kind of weird, just do:

path *.js *.css

Neat, thanks, I’ll clean that up as well.

Apologies if my post buried the lead, but the main thing I’m wondering if there is a better than the following to apply a header to the index pages served by the file_server:

@html {
    path */
    not path /api*
}
header @html {
    Cache-Control "must-revalidate"
}

Well, strictly speaking, this has nothing to do with index pages:

So you could remove that.

Otherwise, the definition of an index page is one which is used when a directory is requested, and directories canonically have a trailing slash, so, that looks good to me otherwise? What don’t you like about it?

The typo is also at the bottom of this header (Caddyfile directive) — Caddy Documentation

(someone reads the doc :smirk:)

1 Like

Thanks, it’s already fixed and will be updated the next time the website is pushed!

2 Likes

Kind of weird? I happen to like the pattern of having one on each line. :cry:

2 Likes

I think the semantics might be different? AND vs. OR?

Nah, the code path for multiple path matchers in the same named matcher explicitly combines them into one path matcher, which is collectively OR’d.

2 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.