Skip to content Skip to sidebar Skip to footer

Defer Primefaces.js Loading

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 JavaSc

Solution 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.


Solution 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.


Post a Comment for "Defer Primefaces.js Loading"