September 19, 2020
Estimated Post Reading Time ~

Add versioning with clientlibs files in AEM

In this article, we will see how to add or remove versioning with clientlibs files(like JavaScript and Cascading Style Sheet (CSS)) in AEM? 
ACS AEM Commons provides a Versioned ClientLibs tool to make serve the clientlibs files(JS and CSS) with postfix with a unique id that is known as long TTLs. Every time you update your JS and CSS file that unique id gets updated for that and because of that AEM server always serves the latest clientlibs files to the browser. It also helps to invalidate the caching at CDN and dispatcher level and ensure every request get served with the latest and updated version of clientlibs file.

To make our clientlibs files versionable we need to do the following configurations in AEM. Install the latest ACS AEM Commons. Download link. This will help us to allow CSS and JavaScript served via AEM ClientLibs to be cached client-side with long TTLs.

Create a Rewriter Configuration Node  
Under the project config folder [/apps/<project name>/config/] create a node rewriter with node type sling:Folder.
Now under the rewriter node, create a node versioned-clientlibs of node type nt:unstructured and add the following highlighted properties on versioned-clientlibs.
You may refer to the default configuration of transformer and versioned-clientlibs under path /libs/cq/config/rewriter/default. 
URI http://localhost:4502/crx/de/index.jsp#/libs/cq/config/rewriter/default
After adding all configurations xml file of versioned-clientlibs will look like this. /apps/<project name>/config/rewriter/versioned-clientlibs/.content.xml

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    jcr:primaryType="nt:unstructured"
    contentTypes="[text/html]"
    enabled="{Boolean}true"
    generatorType="htmlparser"
    order="{Long}1"
    sequence="version"
    serializerType="htmlwriter"
    transformerTypes="[linkchecker,versioned-clientlibs]"/>

You may verify the changes in sling rewriter rules. http://localhost:4502/system/console/status-slingrewriter

versioned-clientlibs configuration in sling rewriter

Apache Configuration
We need to now configure the Apache to send the right header to set a long TTL with clientlibs (JavaScript and CSS) files:
  • SetEnvIf Request_URI "(\.min)?\.[a-f0-9]+\.js" long_expires=true
  • SetEnvIf Request_URI "(\.min)?\.[a-f0-9]+\.css" long_expires=true
  • Header set Cache-Control max-age=2592000 env=long_expires 
How to disable this versioning or TTL from clientlibs files
To disable this feature, in AEM config manager we have a configuration called "ACS AEM Commons - Versioned Clientlibs Transformer Factory".

Note:
This re-writer does NOT support with
  • URIs embedded in CSS or JavaScript, including: background-images, web fonts, etc.
  • Relative URIs, e.g. etc/clientlibs/mysite/styles.css
  • URIs including a scheme, e.g. http://example.com/etc/clientlibs/mysite/styles.css and //example.com/etc/clientlibs/mysite/styles.css
  • URIs to non-AEM HtmlClientLibrary resources, e.g. /etc/designs/mysite.css
  • Tags contained in conditional comments.
  • ClientLibs included by Javascript (e.g. when leveraging the property channels)
References


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.