8
votes

I have tried to add fingerprint suffix to some of my static files (Ex. app.min.js / style.min.css) using ResourceResolvers and ResourceTransformers http://spring.io/blog/2014/07/24/spring-framework-4-1-handling-static-web-resources

I have config the ResourceHandler like this

@Configuration
@EnableWebMvc
public class ResourceResolverConfiguration extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
  registry.addResourceHandler("bower_components/**/*.js", "/assets/**",  "/build/**")
                .addResourceLocations("/bower_components/", "/assets/", "/build/","classpath:/META-INF/webapp/build/")
                .resourceChain(true)
                .addResolver(
                        new VersionResourceResolver()
                            .addContentVersionStrategy("/**")   
                        );
 }
}

and in my main controller I have add some debug log.

logger.debug("js =  '{}'" + this.resourceUrlProvider.getForLookupPath("/build/app.js"));
logger.debug("css = '{}'" + this.resourceUrlProvider.getForLookupPath("/build/styles/style.css"));

After run the web application, from the debug log, there is fingerprint in each file like

app-5d2c76ad6517f26d252d5cc93a4fc7d2.js

and I can access this file directly, (ie. via localhost:8080/build/app-5d2c76ad6517f26d252d5cc93a4fc7d2.js)

However, when I click view source at the web browser, it is still an original file without any fingerprint.

which in my layout.html I load the script/link like this.

 <script th:src="@{/build/app.js}"></script>
 <link th:href="@{/build/styles/style.css}" rel="stylesheet"></link>

I use Thymeleaf for template engine. What should the configuration or code be to make Thymeleaf include the fingerprint files or did I miss something ?

Thank you very much.

2
Was this ever solved?igracia
Does anyone know the corresponding syntax for velocity?Juzer Ali

2 Answers

4
votes

Make sure you're including the appropriate encoding filter, which is what the @{} syntax uses to rewrite URLs:

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

Try this to render the fingerprint along with the filename

<script th:src="${@mvcResourceUrlProvider.getForLookupPath('/build/app.js')}" />

The above results in:

<script src="/build/app-16e85c31092c68733df3c729b831dcfd.js"></script>

I know this is late, but I was looking for the same answer for how to render urls to work with addContentVersionStrategy and this came up, so hopefully it helps someone else.