CQ5 Remove Render-Blocking JavaScript

倾然丶 夕夏残阳落幕 提交于 2019-12-05 18:44:33

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!