Merge multiple JS/CSS files into one in Adobe AEM

Statement : How to merge multiple JS/CSS files into one 


Solution :



  • In this example used proxy clientlibs approach, this is avail­able and rec­om­mend­ed since AEM6.2
  • The AEM Client Library (or ClientLib) func­tion­al­i­ty will man­age all your JavaScript and CSS resources in your appli­ca­tion. It takes cares of depen­den­cy man­age­ment, merg­ing files and mini­fy­ing con­tent (remov­ing unnec­es­sary white spaces).

The fol­low­ing appli­ca­tion sce­nar­ios will be explained:
  • mul­ti­ple com­po­nents with their own JavaScript and CSS files
  • CSS resources have to go in the , JavaScript resources at the end of the page
  • Resources need to be mini­fied
  • chang­ing the mini­fi­ca­tion engine
Let’s get start­ed!

Step 1: Creating components and ClientLib nodes

First we make a few com­po­nents, in this exam­ple 3 com­po­nents are used, we do this all via CRXDE-lite (http://localhost:4502/crx/de).
clientlibs1
Next we are going to add a “clientlib” node of type “cq:ClientLibraryFolder”, inside this node the JavaScript and CSS resources are stored.
clientlibs2
Add a prop­er­ty to every “clientlib” node called “cat­e­gories” of type String[] with the sin­gle val­ue of “myproject.components” (to get a String[] type click the “Mul­ti” but­ton).
clientlibs3
Now add a Boolean prop­er­ty “allow­Proxy”, and set this to true. This will make the clientlibs avail­able via the url /etc.clientlibs/, so it means they are not hav­ing the /apps ref­er­ence.
Your com­po­nents-fold­er will look now like this:
clientlibs4

Step 2: Adding JavaScript and CSS resources

Now we are going to add some sim­ple JavaScript and CSS resources in the “clientlib” nodes.
Cre­ate fol­low­ing files inside of your “clientlib” fold­er of “MyFirst­Com­po­nent”:

first.css

.firstContainer {
    margin-top:10px;
}

first.js

/*
 * This is the comment of the function
 */
function getNameFirst() {
    // return the name
    return "some value";
}

js.txt

# mentions all the JavaScript resources of the ClientLib
first.js

css.txt

# mentions all the CSS resources of the ClientLib
first.css
And repeat the same thing for the oth­er com­po­nents to achieve some­thing that looks like this:
clientlibs5
The con­fig­u­ra­tion of the com­po­nents are now fin­ished.

Step 3: Using ClientLibs in your component

Now the set­up of the ClientLib is fin­ished we can invoke the ClientLibs in your page com­po­nents. When you are using a JSP you can use cq:includeClientlib . In case you are using the HTML Tem­plate Lan­guage (HTL), you can use the data-sly-call to invoke the ClientLib. In this arti­cle HTL will be used for the exam­ples.
We start with putting the fol­low­ing into the ele­ment of our page:
The val­ue of the “cat­e­gories” prop­er­ties of the “clientlib” nodes are “myproject.components”, which is what we need to pro­vide above.
This results in the fol­low­ing HTML-out­put:
This has a few down­sides:
  • 6 serv­er calls have to be made to fetch the resources.
  • Appli­ca­tion struc­ture is exposed.

Step 4: Merging files

To merge the sev­er­al clientlib files into one, we define a clientlibs that is embed­ding the oth­er cat­e­gories. Exam­ple here is tak­en from /app­s/w­ere­tail/­clientlib­s/­clientlib-base.

Step 5: Dependencies

Anoth­er prop­er­ty you can add to the “clientlib” node is “depen­den­cies”, this way you can define depen­den­cies between ClientLibs.
Let’s add a depen­den­cy on “cq.jquery”:
clientlibs8
When you now reload the page the depen­den­cy is writ­ten:

Step 6: Minify and Gzip

To deliv­er a bet­ter per­for­mance you can enable “Mini­fy” and “Gzip” for the “Adobe Gran­ite HTML Library Man­ag­er” (pre­vi­ous­ly also called “Day CQ HTML Library Man­ag­er”), in the Felix Con­fig­u­ra­tion con­sole (http://server/system/console/configMgr). These set­tings are rec­om­mend­ed for pro­duc­tion instal­la­tions.
clientlibs9
By default the YUI com­pres­sor is used when mini­fy­ing files, you can bet­ter use the GCC (Google Clo­jure Com­pil­er) for this.
This is described here: https://helpx.adobe.com/experience-manager/kb/how-to-change-the-minification-engine-for-client-libraries-in-AEM.html

Common error with proxy clientlibs

When you use proxy clientlibs, then you may run into this error:
Unable to proxy yourfont.ttf. No sup­port­ed type for .ttf”, this can hap­pen for types oth­er than JS/CSS.
Rea­son for this is that resources oth­er than JS/CSS need to be put into a fold­er called resources.

FAQ

Q: I don’t want to have all my JavaScript ref­er­ences in the
A: Move the data-sly-call to the right loca­tion in your tem­plate, you can use ClientLib mul­ti­ple times
Q: Where are the gen­er­at­ed files stored in AEM?
A: They are stored in /var/clientlibs
Q: When devel­op­ing I want to have sin­gle file ref­er­ences in my HTML
A: Enable the debug-option in the HTML Library Man­ag­er
Q: Is there a con­sole so I can see the depen­den­cies?
A: Yes, look at this page http://server/libs/cq/ui/content/dumplibs.html
Q: Are there debug­ging options avail­able?
A: Yes, ?debugClientLibs=true writes out sin­gle files
Q: Can I rebuild the ClientLibs?
A: Yes, via this url: /libs/granite/ui/content/dumplibs.rebuild.html
Q: How can I use cache-bust­ing and ClientLibs?
A: You can enable the hash­ing of the url via ‘ver­sioned ClientLibs’.
Q: Do you have an exam­ple of this?
A: Yes, the “core” com­po­nents can be used as a reference: https://git.corp.adobe.com/CQ/aem-core-wcm-components
Q: What is the best prac­tice with regards to per­for­mance and caching
A: Via mod_expires / mod_deflate and the use of cache-bust­ing you can cache the css/js files on the brows­er to increase over­all per­for­mance of your pages. All of this will hap­pen in com­bi­na­tion with the dis­patch­er.

How to embed multiple categories.

In AEM, a category include will merge and compress all the files into a single js (or css) include. This is done per category include.
If you want to merge multiple categories, you should consider using the embed option in categories dependencies. How this works is:


  1. Create a new category (for e.g. cq-embed)
  2. Define embed dependencies ('cq.foundation-main','cq.shared') for the new category you have created.
  3. Reference your new category.

Reference Article : http://blogs.adobe.com/experiencedelivers/experience-management/clientlibs-explained-example/


109 comments:

  1. Hello! I could have sworn I’ve visited your
    blog before but after going through some of
    the articles I realized it’s new to me. Nonetheless, I’m
    certainly delighted I found it and I’ll be bookmarking it
    and checking back regularly!

    ReplyDelete
  2. bookmarked!!, I really like your site!

    ReplyDelete
  3. You've made some really good points there. I looked on the net to find out more about the issue and found most individuals will go along with your views on this web site.

    ReplyDelete
  4. May I simply say what a comfort to discover
    somebody who genuinely knows what they're talking about on the web.

    You certainly know how to bring an issue to light and make it
    important. More people really need to read this and understand this side of the story.
    I was surprised you are not more popular because you surely have the
    gift.

    ReplyDelete
  5. Saved as a favorite, I like your blog!

    ReplyDelete
  6. Good post. I learn something totally new and challenging
    on sites I stumbleupon on a daily basis. It's always exciting to read
    content from other writers and practice a little something from their websites.

    ReplyDelete
  7. Greetings, There's no doubt that your site could be having browser compatibility problems.
    When I take a look at your site in Safari, it looks fine but when opening in IE, it has some overlapping issues.
    I merely wanted to provide you with a quick heads up! Apart
    from that, wonderful website!

    ReplyDelete
  8. Aw, this was an extremely nice post. Spending some time and actual effort to generate
    a good article… but what can I say… I procrastinate a whole lot and
    don't manage to get nearly anything done.

    ReplyDelete
  9. Pretty! This was a really wonderful article.

    Thanks for supplying this information.

    ReplyDelete
  10. Spot on with this write-up, I actually believe this amazing site needs a lot more attention. I’ll probably be back again to read more, thanks for the advice!

    ReplyDelete
  11. Aw, this was an extremely nice post. Taking the time and actual
    effort to make a great article… but what can I say… I put
    things off a lot and don't manage to get anything done.

    ReplyDelete
  12. It’s hard to come by educated people for this subject, but you sound like
    you know what you’re talking about! Thanks

    ReplyDelete
  13. Aw, this was an exceptionally good post. Spending some time and actual
    effort to create a very good article… but what can I say… I hesitate a whole lot and never manage to get anything
    done.

    ReplyDelete
  14. You need to take part in a contest for one
    of the best blogs on the web. I'm going to recommend this blog!

    ReplyDelete
  15. I was able to find good advice from your blog articles.

    ReplyDelete
  16. Hi, I do believe this is a great blog. I stumbledupon it ;) I'm going to revisit yet again since I book marked it.
    Money and freedom is the greatest way to change,
    may you be rich and continue to help others.

    ReplyDelete
  17. May I simply say what a comfort to discover someone who
    really knows what they're talking about online. You
    actually understand how to bring an issue to light and make it important.

    More and more people really need to read this and understand this side of the story.
    It's surprising you are not more popular since you most certainly have the
    gift.

    ReplyDelete
  18. There's certainly a great deal to know about this subject.
    I really like all the points you have made.

    ReplyDelete
  19. I used to be able to find good info from your articles.

    ReplyDelete
  20. Hello! I could have sworn I’ve visited this site before but
    after browsing through a few of the articles I realized it’s new to me.

    Anyhow, I’m definitely pleased I found it and I’ll be book-marking it and checking
    back frequently!

    ReplyDelete
  21. I’m impressed, I have to admit. Rarely do I come
    across a blog that’s both equally educative and engaging, and let me tell
    you, you've hit the nail on the head. The issue is an issue that not enough people are speaking
    intelligently about. Now i'm very happy that I found this in my
    hunt for something concerning this.

    ReplyDelete
  22. You are so cool! I do not believe I've truly read something like this before.
    So good to find somebody with original thoughts on this subject matter.
    Really.. many thanks for starting this up.

    This website is one thing that's needed on the internet,
    someone with some originality!

    ReplyDelete
  23. This is a topic that is near to my heart...
    Thank you! Where are your contact details though?

    ReplyDelete
  24. Howdy! I could have sworn I’ve visited this website before but after going through a few of the posts I realized it’s new to
    me. Nonetheless, I’m definitely happy I stumbled upon it and I’ll be book-marking it and checking back often!

    ReplyDelete
  25. May I simply say what a relief to discover somebody that really knows what they're discussing on the web.
    You certainly understand how to bring an issue to light and make it important.
    A lot more people have to read this and understand this side of your story.

    It's surprising you are not more popular since you definitely have
    the gift.

    ReplyDelete
  26. Excellent article. I will be experiencing many of these issues as well..

    ReplyDelete
  27. This is a very good tip especially to those fresh to the blogosphere.

    Simple but very precise info… Many thanks for sharing this one.

    A must read post!

    ReplyDelete
  28. Way cool! Some very valid points! I appreciate you penning this write-up and the rest of the website is also really good.

    ReplyDelete
  29. I could not resist commenting. Perfectly written!

    ReplyDelete
  30. After going over a handful of the blog posts on your website,
    I honestly appreciate your technique of blogging.
    I saved it to my bookmark webpage list and will be checking
    back soon. Please visit my web site too and let me know your opinion.

    ReplyDelete
  31. Spot on with this write-up, I actually think this website
    needs far more attention. I’ll probably be returning to read through more,
    thanks for the information!

    ReplyDelete
  32. Saved as a favorite, I like your website!

    ReplyDelete
  33. You're so interesting! I don't believe I have read through
    something like this before. So wonderful to find somebody with a few original
    thoughts on this subject matter. Really.. thank you for starting this up.
    This website is one thing that is required on the internet, someone with a bit of originality!

    ReplyDelete
  34. I truly love your blog.. Excellent colors & theme. Did you build this
    amazing site yourself? Please reply back as I’m looking to create my own personal site and would
    love to find out where you got this from or what the theme is called.
    Thanks!

    ReplyDelete
  35. I was excited to discover this site. I need to to thank you for ones
    time for this fantastic read!! I definitely appreciated every little bit of it and
    i also have you book marked to look at new stuff in your website.

    ReplyDelete
  36. Aw, this was a really nice post. Spending some time and actual effort to produce a good article… but
    what can I say… I hesitate a whole lot and never manage
    to get nearly anything done.

    ReplyDelete
  37. Excellent web site you have got here.. It’s hard
    to find high-quality writing like yours nowadays. I really appreciate people like
    you! Take care!!

    ReplyDelete
  38. Excellent site you've got here.. It’s difficult to find quality writing like yours these days.
    I really appreciate individuals like you! Take care!!

    ReplyDelete
  39. I’m amazed, I must say. Seldom do I come across a blog that’s
    both equally educative and amusing, and let me tell you, you have hit the nail on the head.
    The issue is something which not enough people are speaking intelligently about.
    Now i'm very happy I stumbled across this in my search for something
    concerning this.

    ReplyDelete
  40. Spot on with this write-up, I honestly believe this website needs far more attention. I’ll probably be back again to see more,
    thanks for the advice!

    ReplyDelete
  41. Hi, I do think this is a great blog. I stumbledupon it ;) I will come back yet
    again since i have bookmarked it. Money and
    freedom is the greatest way to change, may you be rich and continue to guide other people.

    ReplyDelete
  42. This site was... how do I say it? Relevant!! Finally I've found something
    that helped me. Many thanks!

    ReplyDelete
  43. I’m impressed, I must say. Rarely do I come across a blog that’s both equally educative and interesting, and without a doubt,
    you have hit the nail on the head. The issue is something too few people are speaking intelligently about.
    Now i'm very happy I came across this during my hunt
    for something relating to this.

    ReplyDelete
  44. I have to thank you for the efforts you have put in penning
    this site. I'm hoping to see the same high-grade
    content by you later on as well. In fact, your creative writing abilities has motivated me to get my
    own blog now ;)

    ReplyDelete
  45. Much obliged for your data, it was truly exceptionally helpfull..
    css beautify

    ReplyDelete
  46. I was able to find good information from your
    blog posts.

    ReplyDelete
  47. I used to be able to find good information from your blog articles.

    ReplyDelete
  48. This is a topic which is close to my heart... Cheers!
    Where are your contact details though?

    ReplyDelete
  49. Greetings! Very useful advice in this particular article!
    It's the little changes which will make the
    most significant changes. Thanks for sharing!

    ReplyDelete
  50. Next time I read a blog, Hopefully it does not fail me just as much as this one.
    I mean, I know it was my choice to read through, nonetheless I actually
    thought you would have something helpful to say. All I hear is a bunch of moaning about something you could
    fix if you weren't too busy searching for attention.

    ReplyDelete
  51. Hello there! I could have sworn I’ve been to this website before but after looking at many of the posts I realized it’s new to me.
    Anyways, I’m certainly delighted I came across it and I’ll be book-marking it
    and checking back often!

    ReplyDelete
  52. Having read this I thought it was extremely informative.
    I appreciate you spending some time and energy to put this
    information together. I once again find myself spending a significant
    amount of time both reading and posting comments. But so what, it was still worthwhile!

    ReplyDelete
  53. Good post. I learn something totally new and challenging on sites I stumbleupon on a daily basis.
    It's always interesting to read through content from other writers and practice
    something from other web sites.

    ReplyDelete
  54. I blog often and I genuinely appreciate your content.

    This article has truly peaked my interest.
    I'm going to bookmark your blog and keep checking for new details about once per week.
    I opted in for your Feed too.

    ReplyDelete
  55. An interesting discussion is worth comment. I do believe that
    you should publish more about this subject, it
    might not be a taboo matter but usually people do not discuss such subjects.
    To the next! All the best!!

    ReplyDelete
  56. A fascinating discussion is worth comment.
    I think that you ought to write more about this subject, it might
    not be a taboo matter but generally people don't talk about these topics.

    To the next! Best wishes!!

    ReplyDelete
  57. Hey there! I just wish to offer you a big thumbs up for the great
    info you have got here on this post. I'll be coming back to your blog for
    more soon.

    ReplyDelete
  58. Good web site you have here.. It’s difficult to find
    high-quality writing like yours nowadays. I really appreciate people
    like you! Take care!!

    ReplyDelete
  59. An intriguing discussion is definitely worth comment.
    I do think that you should write more about this topic,
    it might not be a taboo matter but usually folks don't
    talk about such topics. To the next! Cheers!!

    ReplyDelete
  60. I want to to thank you for this fantastic read!! I definitely loved every little bit of it.

    I have got you book-marked to check out new things
    you post…

    ReplyDelete
  61. Howdy! I could have sworn I’ve visited this web site before but after
    going through a few of the articles I realized it’s new to me.

    Regardless, I’m certainly happy I discovered it and I’ll be book-marking it and
    checking back often!

    ReplyDelete
  62. Good post. I certainly appreciate this website.
    Keep it up!

    ReplyDelete
  63. There is certainly a lot to find out about this topic.
    I like all the points you have made.

    ReplyDelete
  64. Spot on with this write-up, I seriously think this site needs a lot more
    attention. I’ll probably be back again to read more, thanks for the advice!

    ReplyDelete
  65. Very good article. I will be experiencing many of these issues as well..

    ReplyDelete
  66. You need to take part in a contest for one of the greatest sites on the internet.
    I most certainly will highly recommend this blog!

    ReplyDelete
  67. Oh my goodness! Impressive article dude! Thank you, However
    I am encountering problems with your RSS. I don’t
    know the reason why I am unable to subscribe to it.
    Is there anyone else getting similar RSS issues?
    Anyone that knows the solution can you kindly respond?
    Thanks!!

    ReplyDelete
  68. Pretty! This was an extremely wonderful post.
    Many thanks for providing this information.

    ReplyDelete
  69. Hi there! This post couldn’t be written much better!

    Reading through this post reminds me of my previous roommate!

    He always kept preaching about this. I will send this post to
    him. Fairly certain he'll have a very good read.
    Thanks for sharing!

    ReplyDelete
  70. Aw, this was an incredibly nice post. Finding
    the time and actual effort to create a good article… but what
    can I say… I hesitate a lot and don't manage to
    get nearly anything done.

    ReplyDelete
  71. I truly love your website.. Excellent colors & theme. Did you
    build this amazing site yourself? Please reply back
    as I’m trying to create my very own blog and would love
    to learn where you got this from or just what the theme is named.
    Cheers!

    ReplyDelete
  72. Greetings! Very useful advice in this particular article!

    It is the little changes that produce the greatest changes.
    Thanks for sharing!

    ReplyDelete
  73. Pretty! This has been an extremely wonderful article. Thanks
    for providing this info.

    ReplyDelete
  74. I need to to thank you for this great read!!
    I definitely loved every little bit of it. I've got you book marked to look at new stuff you post…

    ReplyDelete
  75. Hi there, I believe your blog might be having internet browser compatibility issues.
    Whenever I look at your website in Safari, it looks fine however,
    when opening in IE, it's got some overlapping issues.
    I simply wanted to give you a quick heads up! Aside from that, wonderful website!

    ReplyDelete
  76. I wanted to thank you for this good read!! I absolutely
    loved every little bit of it. I have you book marked to
    look at new things you post…

    ReplyDelete
  77. Spot on with this write-up, I actually believe that this site needs far more attention. I’ll probably be back again to read through more, thanks for the info!

    ReplyDelete
  78. When I initially commented I seem to have clicked on the -Notify
    me when new comments are added- checkbox and from now on each time a comment is added I recieve 4 emails with the same
    comment. There has to be a way you can remove me from that
    service? Thanks a lot!

    ReplyDelete
  79. That is a good tip particularly to those fresh to the
    blogosphere. Simple but very precise information… Many thanks for sharing this
    one. A must read post!

    ReplyDelete
  80. Can I just say what a comfort to discover somebody that actually knows what they're talking about on the web.

    You certainly understand how to bring an issue to light and make it important.
    More and more people should read this and understand this side of your story.
    I was surprised you are not more popular since you definitely possess the gift.

    ReplyDelete
  81. Way cool! Some very valid points! I appreciate you penning this write-up and the rest
    of the site is also very good.

    ReplyDelete
  82. It’s nearly impossible to find well-informed people about this subject,
    however, you seem like you know what you’re talking about!
    Thanks

    ReplyDelete
  83. You've made some good points there. I checked on the web to
    learn more about the issue and found most people will go along with
    your views on this site.

    ReplyDelete
  84. I really like reading through an article that will make
    men and women think. Also, many thanks for allowing me to
    comment!

    ReplyDelete
  85. I quite like reading through an article that will make people think.

    Also, thanks for allowing me to comment!

    ReplyDelete
  86. You need to be a part of a contest for one of the most useful
    blogs on the internet. I'm going to highly recommend this site!

    ReplyDelete
  87. Everything is very open with a really clear explanation of the issues.
    It was really informative. Your website is very helpful.
    Thanks for sharing!

    ReplyDelete
  88. I’m amazed, I have to admit. Rarely do I come across a
    blog that’s both educative and engaging, and let me tell you,
    you have hit the nail on the head. The issue is something not enough men and
    women are speaking intelligently about. I am very happy that I
    found this in my hunt for something relating to this.

    ReplyDelete
  89. Great web site you've got here.. It’s difficult to find excellent writing like yours
    nowadays. I truly appreciate individuals like you!
    Take care!!

    ReplyDelete
  90. After looking at a few of the articles on your web site,
    I seriously appreciate your way of writing a blog.
    I book-marked it to my bookmark site list and will be checking back soon. Please
    check out my website too and tell me how you feel.

    ReplyDelete
  91. Oh my goodness! Awesome article dude! Thank you so much, However
    I am encountering problems with your RSS. I don’t know why I can't subscribe to
    it. Is there anybody else having the same RSS problems? Anyone who knows the answer can you kindly respond?
    Thanx!!

    ReplyDelete
  92. Hi, I do believe your site may be having web browser compatibility problems.
    Whenever I take a look at your website in Safari, it looks fine
    but when opening in I.E., it has some overlapping issues.

    I simply wanted to give you a quick heads up! Other than that,
    great website!

    ReplyDelete
  93. That is a very good tip especially to those new to the blogosphere.
    Brief but very accurate info… Thank you for sharing
    this one. A must read post!

    ReplyDelete
  94. You need to be a part of a contest for one of
    the highest quality websites on the web. I am going to highly
    recommend this blog!

    ReplyDelete
  95. I could not refrain from commenting. Well written!

    ReplyDelete
  96. May I simply say what a relief to uncover an individual who
    truly knows what they are talking about
    online. You definitely realize how to bring a problem to
    light and make it important. More and more people need to read this and
    understand this side of your story. I was surprised
    that you aren't more popular because you certainly have the gift.

    ReplyDelete
  97. Everyone loves it when people come together and share thoughts.
    Great site, stick with it!

    ReplyDelete
  98. You ought to take part in a contest for one of the finest sites on the
    net. I'm going to highly recommend this web site!

    ReplyDelete
  99. You need to be a part of a contest for one of
    the most useful blogs on the web. I am going to highly recommend this
    blog!

    ReplyDelete
  100. I could not resist commenting. Perfectly written!

    ReplyDelete
  101. You made some really good points there. I
    looked on the net for more info about the issue and found most individuals will go along with your views on this website.

    ReplyDelete
  102. After looking at a number of the articles on your website, I honestly appreciate your way of writing a
    blog. I book-marked it to my bookmark webpage list and will be checking back
    soon. Take a look at my web site too and tell
    me what you think.

    ReplyDelete
  103. A motivating discussion is worth comment. I do believe that you need to write more on this subject
    matter, it might not be a taboo matter but generally folks don't speak about these issues.
    To the next! Best wishes!!

    ReplyDelete
  104. Everything is very open with a precise clarification of the
    issues. It was definitely informative. Your website is very helpful.
    Many thanks for sharing!

    ReplyDelete
  105. Very good info. Lucky me I ran across your site by accident (stumbleupon).
    I've book-marked it for later!

    ReplyDelete
  106. Howdy, I think your site could possibly be having internet browser compatibility problems.

    Whenever I look at your site in Safari, it looks fine however
    when opening in Internet Explorer, it's got some overlapping
    issues. I merely wanted to provide you with a quick heads up!
    Besides that, wonderful site!

    ReplyDelete
  107. My brother suggested I might like this blog. He was totally right.
    This post actually made my day. You cann't imagine just how much time I had spent
    for this information! Thanks!

    ReplyDelete
  108. How htmllibraryManager merging code when we add a property embed to the node??

    ReplyDelete