I am trying to load only the specific JavaScript files on a web page in AEM?
For example:-
I have 50 JavaScript for my entire website but I need to load only the 2 Scripts instead of loading the whole 50 scripts. In this case, how do I achieve it?
Solved! Go to Solution.
Views
Replies
Total Likes
Hi Ameen,
From the description how I understand your scenario is ' You have 50 component specific JS files but some pages just need 2 of them in some scenario. You don´t want to load all the 50 together where you use only two ´.
We can achieve this in two ways
1. Using various tagging capabilities of client libraries in AEM. You can create separate category tags for the various groupings of component clientlibs and you can load them on a template level. This is applicable only in scenarios where you have usage of these components are fixed and you have specific templates for these usecases/groupings.
2. You can use a dynamic import logic using the power of ui.frontend module and webpack. Please refer to [1]. Please note this is not an Adobe supported pattern.
The first approach is OOTB, but please note this has an impact on authoring experience as well. Authors needs to change the template properties if add/remove clientlibs OR they are limited to building an experience only with a limited set of components for specific templates.
[1] - https://www.initialyze.com/blog/2020/11/how-to-use-dynamic-imports-with-adobe-aem/
Use component level clientlibs
Is this a good practice for big websites? or any other way to do this? Because if we are loading the entire scripts for a web page, where it only needed two scripts, then the performance might be affected.
Create a common clientlib which required across the webpages or page component based clientlib.
And keep component specific clientlib separately .
And if you want to reuse any existing clientlib then you can embed as well.
Please explain the following line - "Because if we are loading the entire scripts for a web page, where it only needed two scripts" . What is entire script here?
Are you talking about template specific JavaScript files only?
Hi @Ameen_Dev ,
There is no generic solution implementation to put clientlibs, it varies on every usecase.
However, AEM has support to have component level clientlibs, template level clientlibs, embed clientlibs, dependent clientlibs to manage all types of use-cases.
This is the blog which talks about when to use what implementation of clientlibs :
http://www.sgaemsolutions.com/2017/06/clientlibs-in-aem63-part2_25.html
Documentation on clientlibs can be found here :
https://experienceleague.adobe.com/docs/experience-manager-65/developing/introduction/clientlibs.htm...
Hope this helps.
Thanks,
Milind
Hi Ameen,
From the description how I understand your scenario is ' You have 50 component specific JS files but some pages just need 2 of them in some scenario. You don´t want to load all the 50 together where you use only two ´.
We can achieve this in two ways
1. Using various tagging capabilities of client libraries in AEM. You can create separate category tags for the various groupings of component clientlibs and you can load them on a template level. This is applicable only in scenarios where you have usage of these components are fixed and you have specific templates for these usecases/groupings.
2. You can use a dynamic import logic using the power of ui.frontend module and webpack. Please refer to [1]. Please note this is not an Adobe supported pattern.
The first approach is OOTB, but please note this has an impact on authoring experience as well. Authors needs to change the template properties if add/remove clientlibs OR they are limited to building an experience only with a limited set of components for specific templates.
[1] - https://www.initialyze.com/blog/2020/11/how-to-use-dynamic-imports-with-adobe-aem/
It looks like you have all the clientlibs in a single category.
Or you have added all the categories in all the pages.
To overcome this,
You can :