简体   繁体   English

在 Freemarker 文件中导入 Javascript 和 CSS 样式表对性能有何影响?

[英]What is the performance impact of importing Javascript and CSS stylesheets in Freemarker files?

I am working on a project where we use the Freemarker template engine for the front end.我正在做一个项目,我们在前端使用 Freemarker 模板引擎。 A lot of files in the project have a similar structure, in importing some 3rd party libraries and stylesheets like JQuery, Bootstrap, ect.项目中的很多文件都有类似的结构,在导入一些 3rd 方库和样式表时,如 JQuery、Bootstrap 等。 For the sake of consistency, one of the developers suggested having one file as a standardized template containing a header section and scripts.为了保持一致性,其中一位开发人员建议使用一个文件作为标准化模板,其中包含 header 部分和脚本。 And we import this file into all other files.我们将这个文件导入到所有其他文件中。 As a result, any one file into which this template is being imported into will have a lot of unused scripts and stylesheets.因此,这个模板被导入到的任何一个文件都会有很多未使用的脚本和样式表。

Are there any criticisms with this approach, as opposed to importing scripts in a case by case basis?这种方法是否有任何批评,而不是根据具体情况导入脚本? Does it effect performance?会影响性能吗? These scripts are being loaded from some standard CDNs and the app will eventually be hosted on a AWS server.这些脚本是从一些标准 CDN 加载的,应用程序最终将托管在 AWS 服务器上。

No, there is no real criticism with this approach, as it makes use of Freemarker's ability to provide reusable and modular UI.不,这种方法没有真正的批评,因为它利用了 Freemarker 提供可重用和模块化 UI 的能力。 However, this is more of good coding practising than performance.然而,这更多的是良好的编码练习而不是性能。 You should create reusable UI components whenever you could, and header is one main example.应该尽可能创建可重用的 UI 组件,header 就是一个主要示例。

It's good for code integrity and readability to define a template for header header.ftl or head.ftl and import it from all your pages, and even do the same for main menu and footer as well.为 header header.ftlhead.ftl定义模板并将其从所有页面导入,甚至对主菜单和页脚执行相同的操作,对代码完整性和可读性都有好处。

To ramp up performance with Freemarker consider switching on Freemarker caching from Java, XML, or properties config based on your app type.要提高 Freemarker 的性能,请考虑根据您的应用类型从 Java、XML 或属性配置中打开 Freemarker 缓存。 Combining caching with recuction of code size through re-usability will help page loading time.通过可重用性将缓存与减少代码大小相结合将有助于页面加载时间。

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

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