Sling dynamic Include

Sling dynamic Include is an important feature of AEM that allows us to not cache some parts or component on the page.

Suppose, we have a page which is cached and there is one component printing random number which we don’t want to cache on dispatcher.

Sling Dynamic Include helps us to make random number component not to get cache and show latest content.

Follow below steps to implement Sling Dynamic Include:

  1. Use link to download Dynamic Include bundle. Click on Bundle link will allow us to download Jar file.

2. Open bundles using link http://localhost:4503/system/console/bundles and click on below highlighted button to upload downloaded Jar as shown below:

3. Check the checkbox’s and upload the downloaded file. Cick on Install or Update button as shown below:

4. Traverse to configuration page using link http://localhost:4503/system/console/configMgr and look for below highlighted “Apache Sling Dynamic Include — Configuration”. Click on below + button to add the configuration:

5. Add below configuration as part of these configurations:

Enabled → True, will allow us to enable Sling Dynamic Include.

Base path regular expression → Provide content page URL under which we want to have Sling Dynamic Include enable.

Resource Types → Resource Types for which we want to enable Sling Dynamic Include

Add Comment → To Include Components.

Filter Selector → Selector used to mark included resources.

Component TTL → This will allow us to clear cache for component at specific time. We will leave this field blank as we want to clear cache on each page refresh.

We can verify above save configuration inside below location:

/apps/system/config/org.apache.sling.dynamicinclude.Configuration.<hashcode_number>.config# Configuration created by Apache Sling JCR Installer
include-filter.config.ignoreUrlParams=[""]
include-filter.config.resource-types=["practice/components/button"]
include-filter.config.extension=""
include-filter.config.enabled=B"true"
include-filter.config.path="/content/practice"
include-filter.config.required_header="Server-Agent\=Communique-Dispatcher"
include-filter.config.include-type="SSI"
include-filter.config.disableIgnoreUrlParams=B"false"
include-filter.config.selector="nocache"
include-filter.config.appendSuffix=B"true"
include-filter.config.add_comment=B"true"
include-filter.config.rewrite=B"false"
include-filter.config.ttl=""

6. Clearing the dispatcher cache will not show random component.

7. View page source will show below html code snippet.

8. Dispatcher is not able to include or execute below code snippet.<!–#include virtual=”/content/practice/us/en/home/_jcr_content/root/container/container/button_1387403419.nocache.html” –>

9. To make it work, uncomment below highlighted line of code:

10. Add below highlighted line of code as part of httpd.conf file:

<IfModule mod_rewrite.c>

<VirtualHost *:80>

<Directory "C:/Users/imrankhan5/Documents/apache_cache">
<IfModule disp_apache2.c>
ModMimeUsePathInfo On

# enable dispatcher for ALL request. if this is too restrictive,
# move it to another location
SetHandler dispatcher-handler
</IfModule>

Options FollowSymLinks Includes
AllowOverride None

AddOutputFilter INCLUDES .html
</Directory>

ServerName practice.abc
ServerAlias practice.abc

ProxyRequests off
ProxyPreserveHost On
<Location /proxycall.json >
ProxyPass "http://practice.abc/bin/practice/path.json"
ProxyPassReverse "http://practice.abc/bin/practice/path.json"
Order allow,deny
Allow from all
</Location>

LimitRequestFieldSize 32768
AllowEncodedSlashes NoDecode
RewriteEngine On

RewriteRule ^/$ /us/en/home.html [R=301]
RewriteRule ^/content/practice/(.*)$ /$1 [NE,L,R=301]

RewriteCond %{REQUEST_URI} !^/apps
RewriteCond %{REQUEST_URI} !^/bin
RewriteCond %{REQUEST_URI} !^/content
RewriteCond %{REQUEST_URI} !^/etc
RewriteCond %{REQUEST_URI} !^/home
RewriteCond %{REQUEST_URI} !^/libs
RewriteCond %{REQUEST_URI} !^/saml_login
RewriteCond %{REQUEST_URI} !^/system
RewriteCond %{REQUEST_URI} !^/tmp
RewriteCond %{REQUEST_URI} !^/var
RewriteCond %{REQUEST_URI} (.html|.jpe?g|.png|.svg|.gif|.GIF)$
RewriteRule ^/(.+)$ /content/practice/$1 [NC,PT,L]

</VirtualHost>
</IfModule>

11. On restart Apache Server or dispatcher will start to show below output:

Note: Now also on each and every refresh it is showing the same cache content because of below entry where everything is getting cache.

/cache
    {
      /docroot "C:/Users/imrankhan5/Documents/apache_cache"
      /rules
         {
          /0000
            {
             /glob "*"
             /type "allow"
            }
         }
 }

12. Define rule in dispatcher for not to cache content URL’s ending with .nocache.html

13. Add below line of code inside dispatcher.any file for not to cache content URL ending with .nocache.html

/0001 { /glob “*.nocache.html” /type “deny” }

/cache
    {
      /docroot "C:/Users/imrankhan5/Documents/apache_cache"
      /rules
         {
          /0000
            {
               /glob "*"
               /type "allow"
            }
          /0001
            {
               /glob "*.nocache.html"
               /type "deny"
            }
         }
 }

13. Now, on every page refresh it will show latest no cached random number content.

Enable TTL Based caching

Enable TTL based caching will allow us to clear cache at certain amount of time. Follow below steps to enable TTL based caching for Sling Dynamic Include:

  1. Enable TTL based caching using include-filter.config.ttl property will allow us to clear cache at certain amount of time.

It takes time in number of seconds.

We are going to set TTL to 10 seconds as part of current implement as shown below:
include-filter.config.ttl=”10″

Update Component TTL to 10 and click on save.

# Configuration created by Apache Sling JCR Installer
include-filter.config.ignoreUrlParams=[""]
include-filter.config.resource-types=["practice/components/button"]
include-filter.config.extension=""
include-filter.config.enabled=B"true"
include-filter.config.path="/content/practice"
include-filter.config.required_header="Server-Agent\=Communique-Dispatcher"
include-filter.config.include-type="SSI"
include-filter.config.disableIgnoreUrlParams=B"false"
include-filter.config.selector="nocache"
include-filter.config.appendSuffix=B"true"
include-filter.config.add_comment=B"true"
include-filter.config.rewrite=B"false"
include-filter.config.ttl="10"

2. In dispatcher.any file, uncomment below line of code to enableTTL and set it to 1 as shown below:

3. Within rules section under /cache section, remove below line of code form dispatcher.any file as we want to clear cache based on TTL.

/0001 { /glob “*.nocache.html” /type “deny” }

4. Restart the Apache server and refresh page multiple time will show the same content:

5. It will show latest content after every 10 second:

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan