CQ5 Remove JavaScript blocking JavaScript

I am working on this doc to remove js blocking:

Remove JS lock

However, with CQ5, we include js via:

<cq:includeClientLib js="headlibs"/>

      

How can I change the script tag like:

<script async src="my.js">

      

So I can remove the JS blocking.

+3


source to share


1 answer


cq: includeClientLib doesn't have any options for this. You can try using the com.day.cq.widget.HtmlLibraryManager interface to get the path to the JS file, the tag is a convenient wrapper around this interface.

com.day.cq.widget.HtmlLibraryManager clientlibmanager = sling.getService(com.day.cq.widget.HtmlLibraryManager.class);
if(clientlibmanager != null)
{ 
    String[] categoryArray = {"headlibs"};
    java.util.Collection<com.day.cq.widget.ClientLibrary> libs = clientlibmanager.getLibraries(catArray,com.day.cq.widget.LibraryType.JS,false,false);
    for(com.day.cq.widget.ClientLibrary lib : libs) {
        out.write("<script async type=\"text/javascript\" src=\""+lib.getIncludePath(com.day.cq.widget.LibraryType.JS)+"\"></script>");
    }

} else {
         out.write("clientlib manager is null");
  }

      



The method getIncludePath()

also accepts an additional parameter minified (boolean) to specify the path to the minified file.

+4


source







All Articles