Using Client-Side Libraries

You are reading the AEM 6.2 version of Using Client-Side Libraries.
This documentation is also available for the following versions:  AEM 6.3  AEM 6.1  AEM 6.0  AEM 5.6.1  AEM 5.6  CQ 5.5  CQ 5.4 

Modern websites rely heavily on client-side processing driven by complex JavaScript and CSS code. Organizing and optimizing the serving of this code can be a complicated issue.

To help deal with this issue, AEM provides Client-side Library Folders, which allow you to store your client-side code in the repository, organize it into categories, and define when and how each category of code is to be served to the client. The client-side library system then takes care of producing the correct links in your final webpage to load the correct code.

How Client-Side Libraries Work in AEM

The standard way to include a client-side library (that is, a JS or CSS file) in the HTML of a page is simply to include a <script> or <link> tag in the JSP for that page, containig the path to the file in question. For example,

   <script type="text/javascript" src="/etc/clientlibs/granite/jquery/source/1.8.1/jquery-1.8.1.js"></script>

Code samples are intended for illustration purposes only.

While this approach works in AEM, it can lead to problems when pages and their constituent components become complex. In such cases there is the danger that multiple copies of the same JS library may be included in the final HTML output. To avoid this and to allow logical organization of client-side libraries AEM uses client-side library folders.

A client-side library folder is a repository node of type cq:ClientLibraryFolder. It's definition in CND notation is

[cq:ClientLibraryFolder] > sling:Folder
  - dependencies (string) multiple
  - categories (string) multiple
  - embed (string) multiple
  - channels (string) multiple

Code samples are intended for illustration purposes only.

By default, cq:ClientLibraryFolder nodes can be placed anywhere within the /apps, /libs and /etc subtrees of the repository (these defaults, and other settings can be controlled through the Day CQ Library Manager panel of the System Console).

Each cq:ClientLibraryFolder is populated with a set of JS and/or CSS files, along with a few supporting files (see below). The properties of the cq:ClientLibraryFolder are configured as follows:

  • categories: Identifies the categories into which the set of JS and/or CSS files within this cq:ClientLibraryFolder fall. The categories property, being multi-valued, allows a library folder to be part of more than one category (see below for how this may be useful).
  • dependencies: This is a list of other client library categories on which this library folder depends. For example, given two cq:ClientLibraryFolder nodes F and G, if a file in F requires another file in G in order to function properly, then at least one of the categories of G should be among the dependencies of F.
  • embed: Used to embed code from other libraries. If node F embeds nodes G and H, the resulting HTML will be a concetration of content from nodes G and H.

Referencing Client-Side Libraries in HTML

Add a cq:includeClientLib tag to your JSP code to add a link to client libraries in the generated HTML page. To reference the libraries, you use the value of the categories property of the cq:ClientLibrary node.

For example, the /etc/clientlibs/foundation/jquery node is of type cq:ClientLibraryFolder with a categories property of value cq.jquery. The following code in a JSP file references the libraries:

<cq:includeClientLib categories="cq.jquery"/>

Code samples are intended for illustration purposes only.

The generated HTML page contains the following code:

<script type="text/javascript" src="/etc/clientlibs/foundation/jquery.js"></script>

Code samples are intended for illustration purposes only.

For complete information, including attributes for filtering JS, CSS, or theme libraries, see cq:includeClientLib.

Creating Client Library Folders

Create a cq:ClientLibraryFolder node to define JavaScript and Cascading Style Sheet libraries and make them available to HTML pages. Use the categories property of the node to identify the library categories to which it belongs.

The node contains one or more source files that, at runtime, are merged into a single JS and/or CSS file. The name of the generated file is the node name with either the .js or .css file name extension. For example, the library node named cq.jquery results in the generated file named cq.jquery.js or cq.jquery.css

Client library folders contain the following items:

  • The JS and/or CSS source files to merge. 
  • Resources that support CSS styles, such as image files.

    Note: You can use subfolders to organize source files.

  • One js.txt file and/or one css.txt file that identifies the source files to merge in the generated JS and/or CSS files.

For information about requirements that are specific to client libraries for widgets, see Using and Extending Widgets.

The web client must have permissions to access the cq:ClientLibraryFolder node. Generally, you locate the node below /etc/clientlibs in the repository. However, you can also expose libraries from secured areas of the repository (see Embedding Code From Other Libraries, below). 

Overriding libraries in /lib

Client library folders located below /apps take precedence over same-named folders that are similarly located in /libs. For example, /apps/cq/ui/widgets takes precedence over /libs/cq/ui/widgets. When these libraries belong to the same category, the library below /apps is used.

Create a Client Library Folder

  1. Open CRXDE Lite in a web browser (http://localhost:4502/crx/de).

  2. Select the folder where you want to locate the client library folder and click Create > Create Node.

  3. Enter a name for the library file, and in the Type list select cq:ClientLibraryFolder. Click OK and then click Save All.

  4. To specify the category or categories that the library belongs to, select the cq:ClientLibraryFolder node, add the following property, and then click Save All:

    • Name: categories
    • Type: String
    • Value: The category name
    • Multi: Select 
  5. Add source files to the library folder by any means. For example, use a WebDav client to copy files, or create a file and author the content manually.

    Note: You can organize source files in subfolders if desired.

  6. Select the client library folder and click Create > Create file.

  7. In the file name box, type one of the following file names and click OK:

    • js.txt: Use this file name to generate a JavaScript file.
    • css.txt: Use this file name to generate a Cascading Style Sheet.

  8. Open the file and type the following text to identify the root of the path of the source files:


    Replace [root] with the path to the folder that contains the source files, relative to the TXT file. For example, use the following text when the source files are in the same folder as the TXT file:


    The following code sets the root as the folder named mobile below the cq:ClientLibraryFolder node:


  9. On the lines below #base=[root], type the paths of the source files relative to the root. Place each file name on a separate line.

  10. Click Save All.

Linking to dependencies

When the code in your client library folder references other libraries, identify the other libraries as dependencies. In the JSP, the cq:includeClientLib tag that references your client library folder causes the HTML code to include a link to your generated library file as well as the dependencies.

The dependencies must be another cq:ClientLibraryFolder. To identify dependencies, add a property to your cq:ClientLibraryFolder node with the following attributes:

  • Name: dependencies
  • Type: String[]
  • Values: The value of the categories property of the cq:ClientLibraryFolder node that the current library folder depends on.

For example, the /etc/clientlibs/myclientlibs/publicmain has a dependency on the cq.jquery library. The JSP that references the main client library generates HTML that includes the following code:

<script src="/etc/clientlibs/foundation/cq.jquery.js" type="text/javascript">
<script src="/etc/clientlibs/mylibs/publicmain.js" type="text/javascript">

Code samples are intended for illustration purposes only.

Embedding Code From Other Libraries

You can embed code from a client library into another client library. At runtime, the generated JS and CSS files of the embedding library includes the code of the embedded library.

Embedding code is useful for providing access to libraries that are stored in secured areas of the repository. 

App-specific client library folders

It is a best practice to keep all application-related files in their application folder below /app. It is also a best practice to deny access for web site visitors to the /app folder. To satisfy both best practices, create a client library folder below the /etc folder that embeds the client library that is below /app

Use the categories property to identify the client library folder to embed. To embed the library, add a property to the embedding cq:ClientLibraryFolder node, using the following property attributes:

  • Name: embed
  • Type: String[]
  • Value: The value of the categories property of the cq:ClientLibraryFolder node to embed.

Using embedding to minimize requests

In some cases you may find that the final HTML generated for typical page by your publish instance includes a relatively large number of <script> elements, particularly if your site is using client context information for analaytics or targeting. For example, in a non-optimized project you might find the followingseries of <script> elements in the HTML for a page:

<script type="text/javascript" src="/etc/clientlibs/granite/jquery.js"></script>
<script type="text/javascript" src="/etc/clientlibs/granite/utils.js"></script>
<script type="text/javascript" src="/etc/clientlibs/granite/jquery/granite.js"></script>
<script type="text/javascript" src="/etc/clientlibs/foundation/jquery.js"></script>
<script type="text/javascript" src="/etc/clientlibs/foundation/shared.js"></script>
<script type="text/javascript" src="/etc/clientlibs/granite/underscore.js"></script>
<script type="text/javascript" src="/etc/clientlibs/foundation/personalization/kernel.js"></script>

Code samples are intended for illustration purposes only.

In such cases, it can be useful to combine all the required client library code in to a single file so that the number of back and forth requests on page load is reduced. To do this you can embed the required libraries into you app-specific client library using the embed property of the cq:ClientLibraryFolder node.

The following client library categories are incuded with AEM. You should embed only those that are required for he functioning of your particular site. However, you should maintain the order listed here:

  1. browsermap.standard
  2. browsermap
  3. jquery-ui
  4. cq.jquery.ui
  5. personalization
  6. personalization.core
  7. personalization.core.kernel
  8. personalization.clientcontext.kernel
  9. personalization.stores.kernel
  10. personalization.kernel
  11. personalization.clientcontext
  12. personalization.stores
  13. cq.collab.comments
  14. cq.collab.feedlink
  15. cq.collab.ratings
  16. cq.collab.toggle
  18. cq.cleditor

Paths in CSS files

When you embed CSS files, the generated CSS code uses paths to resources that are relative to the embedding library. For example, the publicly-accessible library /etc/client/libraries/myclientlibs/publicmain embeds the /apps/myapp/clientlib client library:


The main.css file contains the following style:

body {
  padding: 0;
  margin: 0;
  background: url(images/bg-full.jpg) no-repeat center top;
  width: 100%;

Code samples are intended for illustration purposes only.

The CSS file that the publicmain node generates contains the following style, using the URL of the original image:

body {
  padding: 0;
  margin: 0;
  background: url(../../../apps/myapp/clientlib/styles/images/bg-full.jpg) no-repeat center top;
  width: 100%;

Code samples are intended for illustration purposes only.

Using a library for specific mobile groups

Use the channels property of a client library folder to identify the mobile group that uses the library. The channels property is useful when libraries of the same category are designed for different device capabilities.

To associate a client library folder with a device group, add a property to your cq:ClientLibraryFolder node with the following attributes:

  • Name: channels
  • Type: String[]
  • Values: The name of the mobile group. To exclude the library folder from a group, prefix the name with an exclamation mark ("!").

For example, the following table lists the value of the channels property for each client library folder of the cq.widgets category:

Client library folder Value of channels property
/libs/cq/analytics/widgets !touch
/libs/cq/analytics/widgets/themes/default !touch
/libs/cq/cloudserviceconfigs/widgets !touch
/libs/cq/searchpromote/widgets !touch
/libs/cq/searchpromote/widgets/themes/default [no value]
/libs/cq/touch/widgets touch
/libs/cq/touch/widgets/themes/default touch
/libs/cq/ui/widgets !touch
/libs/cq/ui/widgets/themes/default !touch

Debugging Tools

AEM provides several tools for debugging and testing client library folders. 

See embedded files

To trace the origin of embedded code, or to ensure that embedded client libraries are producing the expected results, you can see the names of the files that are being embedded at runtime. To see the file names, append the debugClientLibs=true parameter to the URL of your web page. The library that is generated contains @import statements instead of the embedded code.

In the example in the previous Embedding Code From Other Libraries section, the /etc/client/libraries/myclientlibs/publicmain client library folder embeds the /apps/myapp/clientlib client library folder. Appending the parameter to the web page produces the following link in the web page's source code:

<link rel="stylesheet" href="/etc/clientlibs/mycientlibs/publicmain.css">

Code samples are intended for illustration purposes only.

Opening the publicmain.css file reveals the following code:

@import url("/apps/myapp/clientlib/styles/main.css");

Code samples are intended for illustration purposes only.

Use the following procedure to see the names of embedded files:

  1. In the address box of your web browser, append the following text to the URL of your HTML:


  2. When the page loads, view the page source.
  3. Click the link that is provided as the href for the link element to open the file and view the source code.

Discover client libraries

The /libs/cq/ui/components/dumplibs/dumplibs component generates a page of information about all client library folders on the system. The /libs/cq/ui/content/dumplibs node has the component as a resource type. To open the page, use the following URL (use a different host and port as required):


The information includes the library path and type (CSS or JS), and the values of the library attributes, such as categories and dependencies. Subsequent tables on the page show the libraries in each category and channel. 

See generated output

The dumplibs component includes a test selector that displays the source code that is generated for cq:includeClientLib tags. The page includes code for different combinations of js, css, and themed attributes.

  1. Use one of the following methods to open the Test Output page:

    The default page shows output for tags with no value for the categories attribute.

  2. To see the output for a category, type the value of the client library's categories property and click Submit Query. 

Configuring Library Handling for Development and Production

The HTML Library Manager service processes cq:ClientLibraryFolder tags and generates the libraries at runtime. They type of environment, development or production, determines how you should configure the service:

  • Increase security: Disable debugging
  • Improve performance: Remove whitespace and compress libraries. 
  • Improve readability: Include whitespace and do not compress.

For information about configuring the service, see AEM HTML Library Manager.

Any questions?

Have a question about this or any other AEM topic? Ask our Community.
Learn more about AEM topics on our help hub.
Was this helpful?

By submitting your feedback, you accept the Adobe Terms of Use.

Thank you for submitting your feedback.