sraina sraina - 5 months ago 45
CSS Question

CssLinkResourceTransformer is not modifying the URL's present inside css file

I'm using VersionResourceResolver [Spring Boot 1.2.0] for Cache Busting. Using below code, CSS resources gets version number but URL's inside css files doesn't get version even after using CssLinkResourceTransformer.

@Bean
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
return new ResourceUrlEncodingFilter();
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**")
.addResourceLocations("/css/")
.resourceChain(true)
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
.addTransformer(new CssLinkResourceTransformer())
.addTransformer(new AppCacheManifestTransformer());
}

.navbar-header {
height: 58px;
background: transparent url("../images/logo-bg.png") no-repeat left center;
}

GET https://localhost:8443/myapp/css/layout-4a5a9811a2a00e7da9b9c0f0fcfbd343.css
GET https://localhost:8443/myapp/images/logo-bg.png


Anything I'm missing here ? How to version the URL's inside CSS file. Thanks

EDIT:
Sorry I should have provided complete code of addResourceHandlers method which I'm having it now. Please find below the same,

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**")
.addResourceLocations("/css/")
.resourceChain(true)
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
.addTransformer(new CssLinkResourceTransformer())
.addTransformer(new AppCacheManifestTransformer());

registry.addResourceHandler("/fonts/**")
.addResourceLocations("/fonts/")
.resourceChain(true)
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
.addTransformer(new AppCacheManifestTransformer());

registry.addResourceHandler("/html/**")
.addResourceLocations("/html/");

registry.addResourceHandler("/images/**")
.addResourceLocations("/images/")
.resourceChain(true)
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
.addTransformer(new AppCacheManifestTransformer());

registry.addResourceHandler("/js/**")
.addResourceLocations("/js/")
.resourceChain(true)
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
.addTransformer(new AppCacheManifestTransformer());
}

Answer

Your configuration only maps resources that match the pattern "/css/**" and "/images/logo-bg.png" does not match.

Could you try the following?

registry.addResourceHandler("/css/**", "/images/**")
        .addResourceLocations("/css/", "/images/")

Note that to work with each others, resource transformers/resolvers should be declared within a unique resource handler.