我们通过 webapp 中的 wro 提供 javascript 资源(和其他资源)。在 PROD 环境中,浏览器获取(例如)app.js angular webapp 的内容,其中包含未来一年的“过期”标题。
这意味着对于后续请求,浏览器从缓存中获取它而不向服务器请求。如果我们部署新版本的 webapp,浏览器不会获取新版本,因为它是从本地缓存中获取的。
目标是配置 wro 或/和 spring,以便正确设置标头以使浏览器每次都执行请求,并且服务器返回未修改的 304。所以我们会让客户端在新部署时自动“更新”。有人已经做到了吗?
我们使用 Spring 的 Java 配置:
@Configuration
public class Wro4jConfiguration {
@Value("${app.webapp.web.minimize}")
private String minimize;
@Value("${app.webapp.web.disableCache}")
private String disableCache;
@Autowired
private Environment env;
@Bean(name = "wroFilter")
public WroFilter wroFilter() {
ConfigurableWroFilter filter = new ConfigurableWroFilter();
filter.setWroManagerFactory(new Wro4jManagerFactory());
filter.setWroConfigurationFactory(createProperties());
return filter;
}
private PropertyWroConfigurationFactory createProperties() {
Properties props = new Properties();
props.setProperty("jmxEnabled", "false");
props.setProperty("debug", String.valueOf(!env.acceptsProfiles(EnvConstants.PROD)));
props.setProperty("gzipResources", "false");
props.setProperty("ignoreMissingResources", "true");
props.setProperty("minimizeEnabled", minimize);
props.setProperty("resourceWatcherUpdatePeriod", "0");
props.setProperty("modelUpdatePeriod", "0");
props.setProperty("cacheGzippedContent", "false");
// let's see if server-side cache is disabled (DEV only)
if (Boolean.valueOf(disableCache)) {
props.setProperty("resourceWatcherUpdatePeriod", "1");
props.setProperty("modelUpdatePeriod", "5");
}
return new PropertyWroConfigurationFactory(props);
}
}