Thimeleaf and static resources with fingerprints

I tried to add a fingerprint suffix to some of my static files (example. 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 a configHandler 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 add a debug log.

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

After starting the web application from the debug log, there is a fingerprint in each file, for example

application-5d2c76ad6517f26d252d5cc93a4fc7d2.js

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

However, when I click the view source in a web browser, it is still the source file without any fingerprints.

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

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

I am using Thymeleaf for the template engine. What should the configuration or code be for Thymeleaf to include fingerprint files or am I missing something?

Many thanks.

+8
java spring thymeleaf
source share
2 answers

Make sure you enable the appropriate encoding filter that uses the @{} syntax to rewrite URLs:

 @Bean public ResourceUrlEncodingFilter resourceUrlEncodingFilter() { return new ResourceUrlEncodingFilter(); } 
+2
source share

Try to make a fingerprint with the file name

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

The above results:

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

I know this is already late, but I was looking for the same answer how to make the urls to work with addContentVersionStrategy, and it worked out, so hopefully this helps someone else.

+1
source share

All Articles