1

I have a Spring Boot 2 application where static resources are:

src 
|-  main
    |-resources 
        |-static
            |-js/myjs.js
            |-style
                |-css/mycss.css

In my template file:

<link rel="stylesheet" type="text/css" href="/style/css/mycss.css">
<script src="/js/myjs.js"></script>

This is working fine.

However I want to enable browser cache and gzip transfer. To do this I have created the following WebConfig:

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/static/**")
                .addResourceLocations("/static/")
                .setCachePeriod(3600)
                .resourceChain(true)
                .addResolver(new GzipResourceResolver())
                .addResolver(new PathResourceResolver());
    }
}

The app still works but no static content is cached nor gzipped:

enter image description here

Any idea what I'm doing wrong?

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
Vmxes
  • 2,329
  • 2
  • 19
  • 34
  • Isn't Spring confused by the fact that the ResourceHandler and the ResourceLocation are both labelled "static" ? What I think is that your logical ResourceHandler "static" is bypassed by the url ResourceLocation "static". It deserves a try to modify the ResourceHandler "static". – Julien Nov 21 '17 at 13:27
  • Possible duplicate of : https://stackoverflow.com/questions/21123437/how-do-i-use-spring-boot-to-serve-static-content-located-in-dropbox-folder – Julien Nov 21 '17 at 13:30
  • Inspect the request headers to make sure you are accepting gzipped responses. You should have something like `Accept-Encoding: gzip,deflate`. – ESala Nov 21 '17 at 13:46
  • It's there: `Accept-Encoding: gzip, deflate, br` – Vmxes Nov 21 '17 at 13:51

3 Answers3

5

With Spring Boot 2.0.0 I'm using following config:

server.compression.enabled=true
spring.resources.cache.cachecontrol.cache-public=true
spring.resources.cache.cachecontrol.no-cache=false
spring.resources.cache.cachecontrol.no-store=false
spring.resources.cache.cachecontrol.must-revalidate=false
spring.resources.cache.cachecontrol.max-age=31536000
Mamun Sardar
  • 2,679
  • 4
  • 36
  • 44
2
server.compression.enabled=true
spring.resources.cache-period=3600

spring boot 2.0 change

spring.resources.cache-period=3600 to spring.resources.cache.period=3600
Idan Str
  • 614
  • 1
  • 11
  • 33
  • 1
    For a full list of the properties, see https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html – rec Apr 20 '19 at 00:05
1

Finally I was able to solve it with simple application configuration:

server.compression.enabled=true
spring.resources.cache-period=3600

The WebConfig code was removed from the project.

Note: I have to note that Chrome still shows that content was not compressed but if I check the network traffic with Fiddler then it shows that all css and js files were compressed.

Vmxes
  • 2,329
  • 2
  • 19
  • 34