首页 > 解决方案 > 无法使用 Springboot 对静态文件进行版本控制(缓存破坏)

问题描述

我有时会遇到问题,即浏览器使用一些旧的 javascript 文件,即使对文件进行了修改。在开发期间它从未发生过,但在服务器(生产)上它确实发生了。

hier 是我的 application.properties

server.servlet.context-path=/mrr-cms
server.port=8050
spring.cache.type=NONE
spring.resources.chain.cache=false
#Authorization/Licensing (AM/LM)
service.permission.url=https://datlx107
service.permission.endpoint=/AuthorizationManager/service--/endpoint/permissionService
#JWT Token authentication service
service.token.endpoint=/AuthorizationManager/service--/endpoint/tokenService
# Neues Produkt MRR_CMS in der LM/AM Datenbank
service.permission.productName=MRR_CMS

所有其他配置都是 Spring boot 的默认配置。我没有覆盖任何类,例如@config

标签: javaspring-bootspring-mvc

解决方案


您可以创建配置来为静态资源设置自己的缓存设置。

@Configuration
public class CacheWebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/")
          .setCacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS)
            .noTransform()
            .mustRevalidate());
    }
}

请参阅本教程


推荐阅读