Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable browser caching in spring boot

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?

like image 488
Wouter Avatar asked Aug 24 '16 09:08

Wouter


2 Answers

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..

like image 107
Wouter Avatar answered Sep 19 '22 02:09

Wouter


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

like image 26
ETL Avatar answered Sep 22 '22 02:09

ETL