Spring boot cache css and js

168 Views Asked by At

I use spring boot 3 and thymeleaf

I would like to cache css, js and img

@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/js/**", "/resources/css/**", "/resources/img/**")
                .addResourceLocations("/resources/js/", "/resources/css/", "/resources/img/")
                .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
    }
}

I tried with

@EnableWebMvc
@Configuration

but get a 404

only

@Configuration

doesn't cache

1

There are 1 best solutions below

1
inkredusk On

Can you please try something like this?

    @Bean
    public WebMvcConfigurerAdapter webConfigurer () {
          return new WebMvcConfigurerAdapter() {
              @Override
              public void addResourceHandlers (ResourceHandlerRegistry registry) {
                  registry.addResourceHandler("/resources/js/**")
                          .addResourceLocations("/resources/js/")
                          .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
                  registry.addResourceHandler("/resources/img/**")
                  .addResourceLocations("/resources/img/")
                  .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
                  registry.addResourceHandler("/resources/css/**")
                  .addResourceLocations("/resources/css/")
                  .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
              }
          };
    }

Also, recent versions of SpringBoot has certain properties for managing this:

spring.resources.cache.cachecontrol.cache-private= # Indicate that the response message is intended for a single user and must not be stored by a shared cache.
spring.resources.cache.cachecontrol.cache-public= # Indicate that any cache may store the response.
spring.resources.cache.cachecontrol.max-age= # Maximum time the response should be cached, in seconds if no duration suffix is not specified.
spring.resources.cache.cachecontrol.must-revalidate= # Indicate that once it has become stale, a cache must not use the response without re-validating it with the server.
spring.resources.cache.cachecontrol.no-cache= # Indicate that the cached response can be reused only if re-validated with the server.
spring.resources.cache.cachecontrol.no-store= # Indicate to not cache the response in any case.
spring.resources.cache.cachecontrol.no-transform= # Indicate intermediaries (caches and others) that they should not transform the response content.
spring.resources.cache.cachecontrol.proxy-revalidate= # Same meaning as the "must-revalidate" directive, except that it does not apply to private caches.
spring.resources.cache.cachecontrol.s-max-age= # Maximum time the response should be cached by shared caches, in seconds if no duration suffix is not specified.
spring.resources.cache.cachecontrol.stale-if-error= # Maximum time the response may be used when errors are encountered, in seconds if no duration suffix is not specified.
spring.resources.cache.cachecontrol.stale-while-revalidate= # Maximum time the response can be served after it becomes stale, in seconds if no duration suffix is not specified.

You may try using any of the above properties to achieve your solution.