Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Include special attributes in Clientlibs(Defer, Nomodule)

Avatar

Level 2

We have a requirement to include special attributes like async, defer, nomodule to the script tags generated by client libs. We have found there is no out of the box AEM support for this. We tried to find other options. But we are only getting following github links and not getting proper steps to install these extensions. If anyone of you has done such implementation and have an idea to share, kindly let us know.

 

https://github.com/nateyolles/aem-clientlib-async

https://wcm.io/wcm/ui/clientlibs/usage.html

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@smkswamy,

wcm.io is an Open Source project which provides libraries and extensions for AEM-based applications, very established and well known. wcm.io is widely known for it's mocking library for unit/integration. Looking at the documentation, it the client library extension tool looks very promising. Try it out, and let us know how it goes, https://wcm.io/wcm/ui/clientlibs/usage.html

View solution in original post

5 Replies

Avatar

Employee

Hi @smkswamy,

As of now, it isn’t supported in any version of AEM and as per engineering recommendation, its possible using rewriter, if the use case absolutely needs it. GRANITE-26144, GRANITE-18393 are created as an enhancement request for this feature

 

You need to create your own tag which uses the HtmlLibraryManager to get the list of includes for a particular category (or set of categories) and use those to output the appropriate HTML. One way to do this it to create a custom RequestRewriter. This essentially allows you to change what is written to the output HTML for the client lib script tag. This commons package has a code example where a request rewriter is used, and you could do something similar to rewrite the attributes for the script tag: http://adobe-consulting-services.github.io/acs-aem-commons/features/static-reference-rewriter.html.

Also see the following: https://sling.apache.org/documentation/bundles/output-rewriting-pipelines-org-apache-sling-rewriter....

http://www.wemblog.com/2011/08/how-to-remove-html-extension-from-url.html

 

You can also check this https://github.com/nateyolles/aem-clientlib-async for reference. You can use the following to add "async" attribute: <!--/* async */--> <meta data-sly-call="${clientLib.js @ categories='your.clientlib', loading='async'}" data-sly-unwrap></meta>

 

Thanks!!

Avatar

Community Advisor

Please check demo custom implementation to include clientlibs with defer or any other attribute

https://github.com/arunpatidar02/aem63app-repo/blob/master/java/ClientLibsModel.java

 

you can parameterize the implementation for clientlibs category, attribute(s) etc. 



Arun Patidar

Avatar

Correct answer by
Community Advisor

@smkswamy,

wcm.io is an Open Source project which provides libraries and extensions for AEM-based applications, very established and well known. wcm.io is widely known for it's mocking library for unit/integration. Looking at the documentation, it the client library extension tool looks very promising. Try it out, and let us know how it goes, https://wcm.io/wcm/ui/clientlibs/usage.html

Avatar

Level 4

Hi,

I have installed wcm plugin in my local, still the added nonce attribute to the client libs are not showing in the viewsource.

Can someone please suggest on this?

 

Beaula123_0-1717141235576.png

 

 

Avatar

Employee Advisor

@smkswamy  For these additional attributes on the clientlib, there would be two steps involved:

1. Alter the HTML markup by adding additional attributes to the script and link elements

2. Since it's a custom markup, add this logic to a Pojo class to extend the behavior of HtmlLibraryManager's writeIncludes. You can define the allowed attributes as per your case like async, defer, nomodule.

 

This is detailed here: http://www.nateyolles.com/blog/2016/06/custom-aem-html5-async-clientlibs

https://github.com/nateyolles/aem-clientlib-async