Defer primefaces.js loading [duplicate]

半世苍凉 提交于 2019-12-13 19:14:59

问题


The google pagespeed is indicating that loading primefaces.js is slowing down my page:

From the pagespeed report:

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)

How can I resolve this?


回答1:


Adding another solution to this question for anyone else that encounters the same.

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        }

Simply write a custom component for the head tag, then bind it to a renderer that overrides above behavior.

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.




回答2:


You can defer the page load by moving the scripts from the head tag to the end of the body (before closing it), so the scripts start loading after the page markup is loaded, Also be aware that primefaces automatically gets cached so the first request is the longer one the other with use the chached script.



来源:https://stackoverflow.com/questions/19813709/defer-primefaces-js-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!