Defer loading and parsing of PrimeFaces JavaScript files

前端 未结 2 1072
不思量自难忘°
不思量自难忘° 2020-12-02 08:10

While analyzing the performance of a JSF 2.1 + PrimeFaces 4.0 webapp with Google PageSpeed, it recommends among others to defer parsing of JavaScript files. On a test page w

2条回答
  •  庸人自扰
    2020-12-02 08:49

    Initially posted as an answer to Defer primefaces.js loading


    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.

提交回复
热议问题