Include special attributes in Clientlibs(Defer, Nomodule)

Avatar

Avatar

smkswamy

Avatar

smkswamy

smkswamy

10-12-2020

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

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

BrianKasingli

MVP

Avatar

BrianKasingli

MVP

BrianKasingli
MVP

11-12-2020

@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

Answers (3)

Answers (3)

Avatar

Avatar

shelly-goel

MVP

Avatar

shelly-goel

MVP

shelly-goel
MVP

11-12-2020

@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

 

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

3.1K

Likes

1.2K

Correct Reply

883

Avatar

Arun_Patidar

MVP

Total Posts

3.1K

Likes

1.2K

Correct Reply

883
Arun_Patidar
MVP

11-12-2020

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. 

Avatar

Avatar

vanegi

Employee

Avatar

vanegi

Employee

vanegi
Employee

10-12-2020

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!!