Springboot 2.2.0.RELEASE
经过对请求js/css附加md5码或者手动添加版本号方式来保证在js/css内容发生变动时能及时被浏览器加载到:css
spring: thymeleaf: mode: HTML cache: false resources: chain: strategy: content: enabled: true paths: /** enabled: true cache: false static-locations: classpath:/static/
或java
@Configuration public class MvcInterceptorConfig implements WebMvcConfigurer { /** * 功能描述 * <p> * .addFixedVersionStrategy("v1.0.1", "/**") 为手动添加版本号方式 * .addContentVersionStrategy("/**") 为md5码方式 * </p> * * @param registry registry * @return void * @author wandoupeas * @date 2019-11-06 * @since 2019-11-06 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/") .resourceChain(false) .addResolver(new VersionResourceResolver() // .addFixedVersionStrategy("v1.0.1", "/**") .addContentVersionStrategy("/**") ); } }
正常的abc.js浏览器加载时会变成abc-83fb8c4d9199dce0224da0206423106f.js(md5)或/v1.0.1/abc.js(手动添加版本号)spring
<!-- css引用 --> <link th:href="@{/abc.css}" rel="stylesheet"> <link th:href="@{/css/def.css}" rel="stylesheet"> <!-- js引用 --> <script th:src="@{/abc.js}"></script> <script th:src="@{/js/def.js}"></script>
以上方式通常状况下就能够达到需求效果,可是在实际开发过程当中因为相对复杂的场景致使以上配置可能会不生效,经过添加如下Bean
就能解决浏览器
@SpringBootApplication public class XxxApplication { public static void main(String[] args) { SpringApplication.run(XxxApplication.class, args); } /** * 功能描述 * <p> * 添加静态资源md5版本控制 * </p> * * @author wandoupeas * @date 2019-11-06 * @since 2019-11-06 */ @Bean public ResourceUrlEncodingFilter resourceUrlEncodingFilter() { return new ResourceUrlEncodingFilter(); } }
本文使用OpenWrite进行编写