1

I have a simple Spring Boot app with a static content. I would like the browsers to cache not all but just some of the types, like css. So I added following configuration:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/")
                .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
}

But now all the CSS files are server as Content-Type: application/json. Without this code they are served as text/css. I cannot figure out what am I doing wrong. Is there anything I should check or add?

Thanks in advance.

1
  • I've had this exact problem, and it was when the file that was requested did not exist (which can be a typo or an accidentally deleted file). When this happens, Spring returns a json response about the error, but because often 'no-sniff' is enabled, you won't see it in a browser dev tools. Commented Feb 3, 2021 at 15:23

2 Answers 2

0

you have to mention extension of the file name like *.css want to cache

@Configuration
public class BaseMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/css/*.css").addResourceLocations("classpath:/static/").setCachePeriod(24 * 3600 * 365); 
registry.addResourceHandler("/static/js/*.js").addResourceLocations("classpath:/static/").setCachePeriod(0); 

}
} 
Sign up to request clarification or add additional context in comments.

3 Comments

I made myself not clear. The files are cached with this code, but they are not served as text/css anymore but as application/json. And this causes me problems.
i think your missing the type attribute in Html can you please check type="text/css" ex:- <link href="css/main.css" rel="stylesheet" type="text/css">` try below one
The html already contains: <link rel="stylesheet" type="text/css" href="css/test.css">, but I don't think this should do the trick. With the configuration, the response headers for GET css/test.css contain `Content-type: application/json' and expiration in one year. If I remove this configuration, the reposnse headers contain 'Content-type: text/css' and 'Expire: 0' (and several others for not caching the content). So I assume this configuration somehow messes with mime-types, but I am not able to figure out how.
0

I have resolved this by adding @EnableWebMvc annotation and applying this configuration by:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/css/*.css")
            .addResourceLocations("classpath:/static/css/")
            .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS))
            .resourceChain(true).addResolver(new PathResourceResolver());
    registry.addResourceHandler("/js/**")
            .addResourceLocations("classpath:/static/js/")
            .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS))
            .resourceChain(true).addResolver(new PathResourceResolver());
    registry.addResourceHandler("/img/**")
            .addResourceLocations("classpath:/static/img/")
            .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS))
            .resourceChain(true).addResolver(new PathResourceResolver());
    registry.addResourceHandler("/*.html")
            .addResourceLocations("classpath:/static/").setCachePeriod(0);
    registry.addResourceHandler("/*.png")
            .addResourceLocations("classpath:/static/").setCachePeriod(0);

}

@Bean
public InternalResourceViewResolver defaultViewResolver() {
    return new InternalResourceViewResolver();
}

The resolver is important in this case.

I assume that I made some mistakes when using Spring Boot features and the @EnableWebMvc helped a lot.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.