How to send a Cache-Control header only to file with .js?

I need to put a cache-control = “no-cache” only in files .js I need to put “public, max-age=31536000”.

In this topic, It say to use regex. But I tried to use but It didn’t work.
Any ideia how to do this?
Conditionally sending a Cache-Control header?

I know this doesn’t help you right now, but if it’s any consolation, Caddy 2 will make this very easy. :slight_smile:

1 Like

Hi @leonardovff - to help us get an understanding of what you might need to do to fix it, can you tell us in detail:

  1. What did you try? (What Caddyfile, what HTTP requests?)
  2. What result did you expect? (What’s the ideal response?)
  3. What result did you see instead? (Include any error or log output)
1 Like

What I tried:

header / {
Cache-Control “no-cache”
header (.css|.js)$ {
Cache-Control “public, max-age=31536000”

I hope the request for all files have a header with Cache-Control: “no-cache”, only to .js/.css files have another value to Cache-Control → public, max-age=31536000

The second header didn’t work. I think that the path doesn’t accept regexp.

I also think that the path doesn’t accept regexp.

path is the base path to match.

At the moment, I think your best bet is to put your CSS and JS assets in a subfolder (e.g. /js/foo.js, /css/bar.css, etc) and use those subfolders as the header directive’s base path.

The expires plugin can handle regex, but I don’t think it does Cache-Control specifically, just the literal Expires header.

What I understand is that ETag will help to update old files if there is a change in your resource file, so I won’t care to Cache-Control specific files, what I care is nobody mentioning about. ETag is built-in Caddy

I can’t do this - I am using Angular in a version that I can’t modify the js/css build path.

I didn’t know what was ETAG, thank you so much!
Is ETAG for files HTML too?
My problem is with .html - I don’t want that browser storage html in cache.

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