Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CQ5 Remove Render-Blocking JavaScript

I'm working on this document to remove blocking js:

Remove Blocking JS

However with CQ5 we include js via:

<cq:includeClientLib js="headlibs"/>

How can I modify script tag like:

<script async src="my.js">

So I can remove blocking JS.

like image 566
Tien Nguyen Avatar asked Mar 03 '26 09:03

Tien Nguyen


1 Answers

The cq:includeClientLib does not have any options to do this. You can try using the com.day.cq.widget.HtmlLibraryManager interface to get the path of JS file, the tag is a is a convenience wrapper of 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 takes an additional parameter minified (boolean) to give path to the minified file.

like image 199
Sharath Madappa Avatar answered Mar 05 '26 00:03

Sharath Madappa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!