spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.34k stars 38.02k forks source link

Cache busting issue when configured ContentVersionStrategy + CssLinkResourceTransformer #26382

Open didiez opened 3 years ago

didiez commented 3 years ago

When configuring ContentVersionStrategy we are experiencing a problem when a resource referenced in a css file is modified (url with md5 hash changed) but the css file itself remains exactly the same (same md5 hash). We are not getting the last version as the application is rewritting the resource with the same hash (i.e: style-8736f7fc00b943645acfcde4d1456233.css) but the actual content is different (different hashes in the rewritten urls inside de css file).

Are we doing something wrong? Is there a way to compute de md5 hash (ContentVersionStrategy) after content of the css has been transformed by the CssLinkResourceTransformer?

didiez commented 3 years ago

Here's a simple application to reproduce the issue https://github.com/didiez/spring-resources-versioning-issue

bclozel commented 3 years ago

I think that's a limitation of the current design. When resolving paths with the the resource chain, the ContentVersionStrategy is only looking at the actual resource content and not involving the resource transformer chain. Any transformer impacting the content of the resource is not changing the content and the hash of the resource remains the same.

We should take a look at this limitation and see if we can fix this without breaking existing contracts.

As a side note, HTTP caching config and the resolver chain strategies should be enabled in production-specific profiles and left out of the dev profile. I know this is probably done on purpose in this sample, I just wanted to point that out in case this issue is bothering you while working on your app.

Thanks!

didiez commented 3 years ago

Thanks for the quick response :)

Yes, http caching is enabled by default to simplify the sample. We have no issues while developing as these properties are only defined for production. Thanks for pointing it out, though.

rstoyanchev commented 3 years ago

From recollection I think we discussed this at some point. The option I recall is to configure the resource chain and provide a Cache instance to use. This instance can then be used to invalidate the cache. It's a crude mechanism but one option at least.

ealouie commented 7 months ago

Has there been any more consideration of a fix for this issue? My coworkers and I ran into this issue with one of our css files we were caching that is importing from fontawesome.