Skip to content

CssLinkResourceTransformer is incompatible with ContentVersionStrategy when using Caching [SPR-14597] #19166

@spring-projects-issues

Description

@spring-projects-issues

Fabien Chebel opened SPR-14597 and commented

The problem

In our application, we are using a ContentVersionStrategy within Spring's VersionResourceResolver. Spring is serving 2 css files which are in the following hierarchy path:

- themes
|- alpha
   |- style.css
   |- sprite.png
|- beta
   |- style.css
   |- sprite.png

Both style.css files define different classes, so they can safely be included on the same HTML page. Each style.css file references the sprite.png file in the following way:

.alpha { 
    background-image:url("sprite.png");
}

After being processed by the CssLinkResourceTransformer and the VersionResourceResolver components, the css files look like this (assuming I load alpha/style.css in my browser first):

alpha/style.css

.alpha { 
    background-image:url("sprite-md5-of-alpha-sprite.png");
}

beta/style.css

.beta { 
    background-image:url("sprite-md5-of-alpha-sprite.png");
}

As you can see, both file names are being rewritten with the same md5 hash. I expected to get the following result for beta/style.css :

.beta { 
    background-image:url("sprite-md5-of-beta-sprite.png");
}

Problem cause

After debbuging the code for a bit, I noticed that the CssLinkResourceTransformer component forwards the png file name as is (as it appears within the "url()" declaration, so "sprite.png" in my case) to the CachingResourceResolver component via CssLinkResourceTransformer#resolveUrlPath.

After having successfully resolved the url path of the 'sprite.png' file, CachingResourceResolver#resolveUrlPathInternal proceeds to store it in its internal cache using the following cache key:

resolvedUrlPath:sprite.png

The problem is that this cache key is not unique at all. When the CachingResourceResolver is subsequently called to resolve the sprite.png file (from the beta folder), it will return the cached entry that matches the sprite.png file from the alpha folder.

So far I haven't found a reliable fix for this issue.

I know that as a workaround I can rename the 'sprite.png' file to make it unique (thus making the cache key unique), but I may not have that kind of liberty if a library loads multiple resources with the same file name.


Affects: 4.2.7

Reference URL: https://github.com/heffebaycay/spring-css-resource-issue

Issue Links:

0 votes, 5 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions