I'm trying to set up caching headers on specific static file type in Spring Boot. In directory src/main/resources/static there are few subdirectories with different file types:
src/main/resources/static/font --> *.otf
src/main/resources/static/lib --> *.js
src/main/resources/static/images --> *.png, *.jpg
Is there a way to put cache headers by file type inside Spring configuration?
*.otf 365 days
*.png 30 days
*.jpg 7 days
Spring version is 5.2.3 and Spring Boot 2.2.4 - is there a chance that Spring Boot deals with it and makes it not work?
Tried with
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
final CacheControl oneYearPublic =
CacheControl.maxAge(365, TimeUnit.DAYS).cachePublic();
// it does not "work" with "/static/fonts/"
registry
.addResourceHandler("/fonts/{filename:\\w+\\.otf}")
.setCacheControl(oneYearPublic);
}
but I get weird results. When checking with Network tab of DevTools I get these headers:
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
But when I go to the URL directly, I get 404
http://localhost/fonts/1952RHEINMETALL.otf
Without any configuration I get "no-store" Cache-Control header.