Search code examples
jsfjsf-2jsf-2.2jsf-2.3

putting jsf.js at the end of the body


I have a Java EE 8 web application deployed. it uses JSF 2.3 I test it with google PageSpeed Insights. and a recommendation says:

Eliminate render-blocking resources

…font/fonts-min.css.xhtml?ln=custom(**.com)
…fullcalendar/fullcalendar.bundle-min.css.xhtml?ln=vendors(**.com)
…base/vendors.bundle-min.css.xhtml?ln=vendors(**.com)
…base/style.bundle-min.css.xhtml?ln=demo(**.com)
…fix/poppins.css.xhtml?ln=custom(**.com)
/javax.faces.resource/jsf.js.xhtml?ln=javax.faces(**.com)

since jsf.js is added by default by the JSF framework. is there a solution so that I make it load at the end of the body next to other javascripts. And if there is, would that be good idea?

Thanks.


Solution

  • It is possible to place the script to the end of the body using the target="body" attribute:

    <h:head>
        <h:outputScript name="jsf.js" library="javax.faces" target="body"/>
    </h:head>
    

    But as Kukeltje notes, keep in mind that this might have side effects and the optimization gain may be limited to first request.

    The reference implementation (mojarra) explicitly adds this script to the <head> element which might have good reasons:

    com.sun.faces.renderkit.RenderKitUtils.installJsfJsIfNecessary(FacesContext):

    context.getViewRoot().addComponentResource(context, createJsfJs(), "head");