If you don’t have time to investigate what’s best in your situation, then include your client libraries by placing the following HTL lines inside your page head element:

<sly data-sly-use.clientlibs="${'com.adobe.cq.wcm.core.components.models.ClientLibraries' @
    categories='wknd.base', defer=true}">
    ${clientlibs.jsAndCssIncludes @ context="unsafe"}
</sly>

This will include both the CSS and the JS in your page head, but adding the defer attribute to your JS script includes, so that the browsers wait for the DOM to be ready before executing your scripts, and therefore optimizing the page load speed.

Basic Usage

The basic syntax to include both JS and CSS of a client library category, which will generate all the corresponding CSS link elements and/or JS script elements, is as follows:

<sly data-sly-use.clientlibs="${'com.adobe.cq.wcm.core.components.models.ClientLibraries' @ categories='wknd.base'}">
    ${clientlibs.jsAndCssIncludes @ context="unsafe"}
</sly>

To do the same for multiple client library categories at once, an array of strings can be passed to the categories parameter:

<sly data-sly-use.clientlibs="${'com.adobe.cq.wcm.core.components.models.ClientLibraries' @
    categories=['wknd.base', 'cq.foundation']}">
    ${clientlibs.jsAndCssIncludes @ context="unsafe"}
</sly>

CSS or JS Only

Frequently, one wants to place the CSS includes in the HTML head element, and the JS includes just before the closing of the body element.

In the head, to include only the CSS, and not the JS, use cssIncludes:

<sly data-sly-use.clientlibs="${'com.adobe.cq.wcm.core.components.models.ClientLibraries' @ categories='wknd.base'}">
    ${clientlibs.cssIncludes @ context="unsafe"}
</sly>

Before the body closing, to include only the JS, and not the CSS, use jsIncludes:

<sly data-sly-use.clientlibs="${'com.adobe.cq.wcm.core.components.models.ClientLibraries' @ categories='wknd.base'}">
    ${clientlibs.jsIncludes @ context="unsafe"}
</sly>

Attributes

To apply attributes to the generated CSS link elements and/or JS script elements, a number of parameters are possible:

<sly data-sly-use.clientlibs="${'com.adobe.cq.wcm.core.components.models.ClientLibraries' @
    categories='wknd.base',
    media='print',
    async=true,
    defer=true,
    onload='console.log(\'loaded: \' + this.src)',
    crossorigin='anonymous'}">
    ${clientlibs.jsAndCssIncludes @ context="unsafe"}
</sly>

CSS link attributes that can be passed to jsAndCssIncludes and cssIncludes:

  • media: string JS script attributes that can be passed to jsAndCssIncludes and jsIncludes:
  • async: boolean
  • defer: boolean
  • onload: string
  • crossorigin: string