简体   繁体   English

带指纹的胸腺和静态资源

[英]Thymeleaf and Static Resources with fingerprint

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 我尝试使用ResourceResolvers和ResourceTransformers http://spring.io/blog/2014/07/24/spring-将指纹后缀添加到某些静态文件(例如,app.min.js / style.min.css)中。 框架4-1处理静态Web资源

I have config the ResourceHandler like this 我已经像这样配置ResourceHandler

@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 运行Web应用程序后,在调试日志中,每个文件中都有指纹,例如

app-5d2c76ad6517f26d252d5cc93a4fc7d2.js app-5d2c76ad6517f26d252d5cc93a4fc7d2.js

and I can access this file directly, (ie. via localhost:8080/build/app-5d2c76ad6517f26d252d5cc93a4fc7d2.js) 而且我可以直接访问此文件(即通过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. 但是,当我在Web浏览器上单击查看源时,它仍然是没有任何指纹的原始文件。

which in my layout.html I load the script/link like this. 我在layout.html文件中这样加载脚本/链接。

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

I use Thymeleaf for template engine. 我将Thymeleaf用于模板引擎。 What should the configuration or code be to make Thymeleaf include the fingerprint files or did I miss something ? 要使Thymeleaf包含指纹文件,应该使用什么配置或代码?或者我错过了什么?

Thank you very much. 非常感谢你。

Make sure you're including the appropriate encoding filter, which is what the @{} syntax uses to rewrite URLs: 确保您包含适当的编码过滤器,这是@{}语法用于重写URL的内容:

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

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. 我知道这很晚了,但是我一直在寻找相同的答案,以了解如何呈现与addContentVersionStrategy一起使用的url,这很有用,因此希望对其他人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM