简体   繁体   中英

Cache CSS , JavaScript , Image Spring Boot (+Thymeleaf)

I'm building a web Spring Boot with Thymeleaf. I saw Thymeleaf always load all resource when I refresh or change page like this Thymeleaf . How can I cache resources from memory, similar to .Net MVC framework which can do it like this DotNetMVC

There are two ways of doing this.

  1. using WebMVCConfig.

  2. using application.properties file

WebMVCConfig.

Create template resolver as this.

@Bean
public TemplateResolver templateResolver()
{
    FileTemplateResolver templateResolver = new FileTemplateResolver();
    templateResolver.setTemplateMode( "HTML5" );
    templateResolver.setCacheable( Boolean.FALSE );
    templateResolver.setOrder( 1 );
    return templateResolver;
}

Application properties

add following line to application.properties file

spring.thymeleaf.cache: false

I saw other solution

@Bean
    public WebMvcConfigurer configurer () {
        return new WebMvcConfigurer() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/resources/**")
                        .addResourceLocations("/resources/")                  
                        .setCachePeriod(31556926);
            }
        };
    }

For reference: headers-cache-control But still doesn't work for me. I am using Spring Boot Security also.

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