简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM