Skip to content Skip to sidebar Skip to footer

Primefaces Javascript Defer Parsing

Primefaces 4.0 is generating lots of overhead during page loading as seen from PageSpeed Insights: **605.3KiB of JavaScript is parsed during initial page load. Defer parsing JavaSc

Solution 1:

I got the moving of the scripts to work with the followind snippet:

publicclassScriptValidateListenerimplementsSystemEventListener {

    @OverridepublicvoidprocessEvent(SystemEvent event)throws AbortProcessingException {
        UIViewRootroot= (UIViewRoot) event.getSource();
        FacesContextctx= FacesContext.getCurrentInstance();
        List<UIComponent> resources = root.getComponentResources(ctx, "HEAD");
        for (UIComponent r : resources) {
            Stringname= (String) r.getAttributes().get("name");
            if (name == null) {
                continue;
            }

            if (name.contains(".js")) {
                root.removeComponentResource(ctx, r, "HEAD");
                root.addComponentResource(ctx, r, "BODY");
            }
        }
    }

    @OverridepublicbooleanisListenerForSource(Object source) {
        return (source instanceof UIViewRoot);
    }
}

This moves all javascripts from HEAD to end of the BODY. But. There is this problem with Primefaces that the components rendered will try to access either JQuery ($.) or PrimeFaces javascript functions and that will break all ajax functionality on the page. Propably I will need to decide what of the scripts to move and what not to move. Also a part from the Listener I needed to define the following to faces-config.xml to make it work:

<application>
    <system-event-listener>
        <system-event-listener-class>com.example.listener.ScriptValidateListener</system-event-listener-class>
        <system-event-class>javax.faces.event.PreRenderViewEvent</system-event-class>
    </system-event-listener>
</application>

Post a Comment for "Primefaces Javascript Defer Parsing"