3

I'm developping a angularjs application with Spring.

I often have to change my html/javascript file and I noticed that spring is caching static contents. How can I disable that?

I already tried this ...

@Configuration
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
class WebMvcConfig extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {

    @Autowired
    private Environment env;

    @Bean
    public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
        return new ResourceUrlEncodingFilter();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        boolean devMode = this.env.acceptsProfiles("dev");
        //boolean useResourceCache = !devMode;
        boolean useResourceCache = false;
        Integer cachePeriod = devMode ? 0 : null;

        registry.addResourceHandler("/public/**")
                .addResourceLocations("/public/", "classpath:/public/")
                .setCachePeriod(cachePeriod)
                .resourceChain(useResourceCache)
                .addResolver(new GzipResourceResolver())
                .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
                .addTransformer(new AppCacheManifestTransformer());
    }

}

and that ...

WebContentInterceptor webContentInterceptor;
public @Bean WebContentInterceptor webContentInterceptor () {
    if (this.webContentInterceptor == null) {
        this.webContentInterceptor = new WebContentInterceptor();

        this.webContentInterceptor.setAlwaysUseFullPath (true);
        this.webContentInterceptor.setCacheSeconds (0);


        this.webContentInterceptor.setCacheMappings (new Properties() {
            private static final long serialVersionUID = 1L;

            {
                put ("/styles/**", "0");
                put ("/scripts/**", "0");
                put ("/images/**", "0");
                put ("/js/**", "0");
            }
        });
    }

    return this.webContentInterceptor;
}

this is my build.gradle file

group 'xyz'
version '1.0-SNAPSHOT'
buildscript{
    repositories{
        mavenCentral()
    }
    dependencies{
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repository.jboss.org/nexus/content/repositories/releases" }
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.security:spring-security-web:4.0.3.RELEASE'
    compile 'net.sf.dozer:dozer:5.4.0'

    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'com.h2database:h2'// For Testing purpose
    compile 'com.google.guava:guava:19.0' // google library for data collections

    testCompile("junit:junit")
    //testCompile group: 'junit', name: 'junit', version: '4.11'
}

task wrapper(type: Wrapper){
    gradleVersion = '2.3'
}

configurations.all {
    // https://stackoverflow.com/questions/14024756/slf4j-class-path-contains-multiple-slf4j-bindings/25694764#25694764
    exclude module: 'slf4j-log4j12'
}
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
robie2011
  • 3,678
  • 4
  • 21
  • 20

4 Answers4

7

Just put this configuration option into your application.properties:

spring.resources.chain.cache=false # Disable caching in the Resource chain.

You may want to also take a look at more fine grained config options related to Static content served by Spring Boot (scroll down to section # SPRING RESOURCES HANDLING).

Additionally, there may be static resources cached by infrastructure that is not handled by Spring Boot and it's container (e.g Web Browser). If you want to overcome this type of caching, there is option to use technique called cache busting. Read this section of Spring Boot docs to get more info about it.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • it doesn't work for me somehow. I also activated 'spring-boot-devtools', but it doesn't helped me. I'm using chrome in incognito mode and everything is localhost so it shouldn't be a infrastructure issue. But it's interessting to know that on my mac the cache will be refreshed after every server restart but on my windows machine it cache it for approx. 24h. ... same application – robie2011 Apr 13 '16 at 10:08
1

It was a silly mistake. I was editing the HTML/CSS/JS source files but the compiled and deployed version was not affected from this modification.

robie2011
  • 3,678
  • 4
  • 21
  • 20
1

To see not compiled but the very version of static content you're editing set in application.yml something like:

spring:
  profiles: dev
  resources:
    static-locations: file:src/main/resources/static

But do so only in dev profile to avoid problem after deployment.

Sasha77ru
  • 23
  • 4
  • 1
    How does this stop caching of static files? – Nikhil Sahu Dec 04 '19 at 13:11
  • @Nikhil I suppose Spring Security usually does such magic. Otherwise it's possible to set headers using spring.resources.cache.cachecontrol: no-store, no-cache, max-age=0 e.t.c. – Sasha77ru Dec 04 '19 at 19:46
0

Add this is in application.properties

  1. Use this if you want to cache for a particular amount of time

    spring.resources.cache.cachecontrol.cache-public=true spring.resources.cache.cachecontrol.max-age=2000 #(2 seconds)

  2. Use this if you don't want any cache

    spring.resources.cache.cachecontrol.no-cache=true

But try to adopt first way, as it is not a good practice to load static content every time from server

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
Abhijeet Gite
  • 79
  • 1
  • 6