Recently I introduced the new ResourceTransformer into our application for JS and CSS files. In my WebConfig I overrode the addResourceHandlers method. and added the following code:
@Override
public void addResourceHandlers( ResourceHandlerRegistry registry ) {
boolean localMode = this.env.acceptsProfiles( "local" );
//String location = localMode ? "file:///" + getProjectRootRequired() + "/client/src/" : "classpath:/static/";
String location = "classpath:/static/";
Integer cachePeriod = this.resourceProperties.getCachePeriod();
boolean useResourceCache = !localMode;
String version = localMode ? "local" : this.appVersion;
AppCacheManifestTransformer appCacheTransformer = new AppCacheManifestTransformer();
LessLinkResourceTransformer lessLinkResourceTransformer = new LessLinkResourceTransformer();
VersionResourceResolver versionResolver = new VersionResourceResolver()
//.addFixedVersionStrategy( version, "/**/*.js" ) //Enable this if we use a JavaScript module loader
.addContentVersionStrategy( "/**" );
//A Handler With Versioning
registry.addResourceHandler( "/**/*.js", "/**/*.css", "/**/*.less", "/**/*.png", "/**/*.gif", "/**/*.jpg", "/**/*.map" )
.addResourceLocations( location )
.setCachePeriod( this.fingerprintedCachePeriod )
.resourceChain( useResourceCache )
.addResolver( versionResolver )
.addTransformer( appCacheTransformer )
.addTransformer( lessLinkResourceTransformer );
}
I added the ResourceUrlEncodingFilter as it says to do in the Spring 4.1 docs. Then when I tested my app I realized that the CSS files were being versioned, but the JS files were not being versioned. After a lot of debugging, I realized that the problem was in ResourceUrlEncodingFilter.getForLookupPath. It loops through all the handler mappings and uses the first one that matches with AntMatcher. My CSS links were being matched to my mapping that I added in my WebConfig, but my JS links were always getting picked up by another mapping ("/") that did not include a version transformer. I finally tracked this mapping to the one that was added in WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.addResourceHandlers. This was a huge pain since the ResourceHandlerRegistry API does not allow you to remove existing handlers, but only add new ones. I didn't want a to overwrite "/" since I didn't want all my links transformed. The only temporary solution I could come up with was to make my WebConfig extend WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter so that WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.addResourceHandlers would not get called. Then I added another ResourceHandler to my addResourceHandlers for the static resources I did not want versioned.
I don't think this is a good long term solution for us, but it works for the mean time.
Recently I introduced the new ResourceTransformer into our application for JS and CSS files. In my
WebConfigI overrode theaddResourceHandlersmethod. and added the following code:I added the
ResourceUrlEncodingFilteras it says to do in the Spring 4.1 docs. Then when I tested my app I realized that the CSS files were being versioned, but the JS files were not being versioned. After a lot of debugging, I realized that the problem was inResourceUrlEncodingFilter.getForLookupPath. It loops through all the handler mappings and uses the first one that matches with AntMatcher. My CSS links were being matched to my mapping that I added in myWebConfig, but my JS links were always getting picked up by another mapping ("/") that did not include a version transformer. I finally tracked this mapping to the one that was added inWebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.addResourceHandlers. This was a huge pain since theResourceHandlerRegistryAPI does not allow you to remove existing handlers, but only add new ones. I didn't want a to overwrite "/" since I didn't want all my links transformed. The only temporary solution I could come up with was to make myWebConfigextendWebMvcAutoConfiguration.WebMvcAutoConfigurationAdapterso thatWebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.addResourceHandlerswould not get called. Then I added anotherResourceHandlerto myaddResourceHandlersfor the static resources I did not want versioned.I don't think this is a good long term solution for us, but it works for the mean time.