After following a tutorial on using a content version strategy in Spring for static assets, everything is working correctly except for one corner case that I'm facing:
Within my HTML, there is a <link>
referencing a CSS file called a.css
. Upon inspecting the HTML returned by the server, I noticed that the link has been transformed to a-(md5).css
, which is expected. The issue arises because a.css
also imports another CSS file named b.css
. Spring properly updates the import from @import '/css/b.css'
to @import '/css/b-(md5).css'
. However, when I update b.css
, the browser caches the request of a-(md5).css
since the md5 of a.css
remains the same, leading to incorrect styling due to the pointer still pointing to the previous b-(old-md5).css
This seems like a common issue. How can this be resolved?
Is there a way to instruct the version strategy to compute the md5 after resolving links so that if the dependency's md5 changes, it would also affect the dependent's md5?
Below is my current configuration:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//content-based versioning and max caching
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/")
.setCacheControl(MAX_CACHE_DURATION)
.resourceChain(false)
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
.addTransformer(new CssLinkResourceTransformer());
//no cache
registry.addResourceHandler("/*.html").setCacheControl(CacheControl.noCache());
}