Client libraries in AEM are used to manage and bundle client-side resources such as JavaScript, CSS, and images. They allow developers to organize and reuse front-end code across multiple pages and templates in AEM. In this section, we will discuss how to create a client library in AEM.
Creating a Client Library in AEM:
- Create a new folder for the client library under the
/appsdirectory. The convention for naming client library folders isclientlibs.<name>.For example, to create a client library namedmyclientlib, create a new folder/apps/clientlibs.myclientlib. - Inside the
myclientlibfolder, create ajsandcsssubfolder. These subfolders will hold the JavaScript and CSS files that are part of the client library.For example, create folders/apps/clientlibs.myclientlib/jsand/apps/clientlibs.myclientlib/css. - Create a
clientlibnode under themyclientlibfolder using thent:foldernode type. This node will hold the client library definition.For example, create a new node/apps/clientlibs.myclientlib/clientlibusing thent:foldernode type. - Set the properties of the
clientlibnode to define the client library. The following properties can be set:jcr:title: The title of the client library.jcr:description: A description of the client library.categories: An array of categories that the client library belongs to. Categories can be used to group client libraries and control their loading order.allowProxy: A Boolean flag indicating whether the client library can be served by a reverse proxy.
For example, to set the title and categories of the
myclientlibclient library, set the following properties:jcr:title = My Client Library categories = [mycategory]
Note: The
categoriesproperty is optional, but it is recommended to use it to group and organize client libraries. - Add the JavaScript and CSS files to the client library. To do this, create a
categoriesnode under theclientlibnode, using thent:foldernode type.For example, create a new node
/apps/clientlibs.myclientlib/clientlib/categoriesusing thent:foldernode type. - Add the JavaScript and CSS files to the
categoriesnode using thecq:includenode type. Thecq:includenode has the following properties:file: The path to the JavaScript or CSS file to include.path: An alternative path to the JavaScript or CSS file. This can be used to include files from outside of the client library folder.
For example, to include a JavaScript file named
myscript.jsand a CSS file namedmystyle.css, create the following nodes:/apps/clientlibs.myclientlib/js/myscript.js /apps/clientlibs.myclientlib/css/mystyle.css /apps/clientlibs.myclientlib/clientlib/categories/js cq:include (nt:file) file = /apps/clientlibs.myclientlib/js/myscript.js /apps/clientlibs.myclientlib/clientlib/categories/css cq:include (nt:file) file = /apps/clientlibs.myclientlib/css/mystyle.css - Include the client library in your AEM pages or templates. To do this, use the
clientlibSling tag in your JSP files.For example, to include the `myclient