延迟primefaces.js加载 [英] Defer primefaces.js loading
问题描述
google pagespeed表示加载primefaces.js正在减慢我的页面的速度:
The google pagespeed is indicating that loading primefaces.js is slowing down my page:
从pagespeed报告中:
From the pagespeed report:
延迟解析JavaScript 通过最小化JavaScript的数量 需要呈现页面,并推迟不必要的解析 JavaScript,直到需要执行为止,您可以减少初始 页面的加载时间.
Defer parsing of JavaScript By minimizing the amount of JavaScript needed to render the page, and deferring parsing of unneeded JavaScript until it needs to be executed, you can reduce the initial load time of your page.
Suggestions for this page
284.3KiB of JavaScript is parsed during initial page load. Defer parsing JavaScript to reduce blocking of page rendering.
https://www.mysite.co.uk/.../primefaces.js.xhtml?... (185.4KiB)
https://www.mysite.co.uk/.../jquery.js.xhtml?... (95.1KiB)
我该如何解决?
推荐答案
为遇到此问题的其他任何人添加另一个解决方案.
Adding another solution to this question for anyone else that encounters the same.
您将需要自定义撇号HeadRenderer
,以实现建议的页面速度建议.尽管这可能是PrimeFaces可以实现的,但我在v5.2.RC2中看不到它.这些是encodeBegin
中需要更改的行:
You will need to customize the primefaces HeadRenderer
to achieve the ordering pagespeed recommends. While this is something that could have been implemented by PrimeFaces, I do not see it in v5.2.RC2. These are the lines in encodeBegin
that need change:
96 //Registered Resources
97 UIViewRoot viewRoot = context.getViewRoot();
98 for (UIComponent resource : viewRoot.getComponentResources(context, "head")) {
99 resource.encodeAll(context);
100 }
只需为head
标签编写一个自定义组件,然后将其绑定到覆盖上述行为的渲染器.
Simply write a custom component for the head
tag, then bind it to a renderer that overrides above behavior.
现在,您不想只为更改而复制整个方法,添加一个名为"last"的构面并将脚本资源作为新的deferredScript
组件移至渲染器中的开头可能更干净.让我知道是否有兴趣,我将创建一个分叉来演示操作方法.
Now you wouldn't want to duplicate the entire method just for this change, it may be cleaner to add a facet called "last" and move script resources to its beginning in your renderer as new deferredScript
components. Let me know if there's interest, and I'll create a fork to demonstrate how.
这种方法是未来证明"的,因为这种方法不会随着向组件中添加新资源依赖项或向视图中添加新组件而中断.
This approach is "future proof" in the sense that it doesn't break as new resource dependencies are added to components, or as new components are added to the view.
这篇关于延迟primefaces.js加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!