7

I'm trying to get spring boot let the browser cache static resources. My resources are located in the classpath under "static". When I look at the headers sent back, I see the modification headers being set fine, but somehow the header "Cache-Control: no-store" is also added.

HTTP/1.1 200
Last-Modified: Wed, 24 Aug 2016 08:50:16 GMT
Cache-Control: no-store
Accept-Ranges: bytes
Content-Type: text/css
Content-Length: 434554
Date: Wed, 24 Aug 2016 09:42:42 GMT

I have already seen this answer How to enable HTTP response caching in Spring Boot, but this doesn't seem to apply to me as I am not using spring-security, it is not on the classpath.

I am using spring-boot 1.4.0 with thymeleaf.

So, how do I let spring boot not include the Cache-Control header?

Community
  • 1
  • 1
Wouter
  • 3,976
  • 2
  • 31
  • 50
  • Even though the linked answer does talk about Spring Security, did you try any of the answers? For example, the last code fragment of the accepted answer is not about Spring Security. – g00glen00b Aug 24 '16 at 09:56
  • _"the header "Cache-Control: no-cache""_ Your example says "no-store", which is not the same. And "no-store" is security related. – a better oliver Aug 24 '16 at 10:10
  • @g00glen00b only the answer with the external lib seem elligible, but I would think this should be solvable within spring-boot itself.. – Wouter Aug 24 '16 at 10:14
  • @zeroflagL thanks for pointing out the typo. It should be no-store. – Wouter Aug 24 '16 at 10:15
  • @Wouter the last code block of the accepted answer is about configuring `WebMvcConfigurerAdapter`, which is not related to Spring Security. – g00glen00b Aug 24 '16 at 10:15
  • @g00glen00b Tinkered a bit with the solution and found the answer. Posting it now. – Wouter Aug 24 '16 at 10:27

2 Answers2

7

Turns out it is fairly easy to resolve.

The directory structure is classpath:/static/assets. To have no cache-control header added to the responds, add this class:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/static/assets/").setCacheControl(CacheControl.empty());
    }
}

It still baffled me that "no-store" is the default with spring-boot..

Wouter
  • 3,976
  • 2
  • 31
  • 50
5

At least in recent (2018) versions of SpringBoot, there are properties you can set:

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.

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

ETL
  • 9,281
  • 3
  • 29
  • 28