Blogs

Sling Dynamics Include in AEM
Sling Dynamic Include (SDI): Dynamically Include Page Components

 

In CQ or AEM, most of the pages remain static. Hence, caching of the pages is very useful with dispatchers or any other available AEM plugins/connectors. Imagine a scenario where homepage of news agency must show the hot news which is different for different regions, however, because of caching it is displaying the same news in all the regions. Strange! To rescue from these, live scenarios, the application may require certain elements/components of the page to be dynamically included. In AEM, Sling Dynamic Include (SDI) provides this functionality.

 

Let’s elaborate SDI integration with AEM 6.4, Dynamic Include 3.0.0 and Dispatcher 2.4.

 

Please note that Step 1 and Step 2 need to be performed on publish instance.

 

Step 1:

 

Install Sling Dynamic Include Bundle using the following steps:

  1. Download the Dynamic Include bundle.
  2. Open the bundles using http://<host>:<port>/system/console/bundles
  3. Click on install/update button in the right corner of the screen

      4. Check the Start Bundle checkbox and browse the location where the bundle is downloaded and Click on install/update button

Once the installation of the bundle is completed, verify it by searching Dynamic Include. It should be in an active state.

Step 2:

 

After installation of the SDI bundle, the next step is to configure the component to be dynamically included.

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0"xmlns:cq="http://www.day.com/jcr/cq/1.0"
    xmlns:jcr="http://www.jcp.org/jcr/1.0"xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    jcr:primaryType="sling:OsgiConfig"
    include-filter.config.enabled="{Boolean}true"
    include-filter.config.path="/content"
    include-filter.config.resource-types="[my-app/components/content/dynamic_included_component]"
    include-filter.config.include-type="SSI"
    include-filter.config.add_comment="{Boolean}false"
    include-filter.config.selector="nocache"
    include-filter.config.ttl=""
    include-filter.config.required_header="Server-Agent=Communique-Dispatcher"
    include-filter.config.ignoreUrlParams="[]"
    include-filter.config.rewrite="{Boolean}true"
/>

Please find below the brief description of each OSGI config used above:

  • enabled – set it to true to enable SDI.
  • path – SDI configuration will be enabled only for this path.
  • resource-types – which components should be replaced with tags
  • include-type – type of include tag (Apache SSI, ESI or Javascript)
  • Apache SSI – Apache Server Side Includes

Apache HTTP Server is set up as a caching proxy in front of the AEM. This means that the include will be done by the http server and not by the sling engine.

  • ESI – Edge Site Includes

Edge Site Includes can be used as an alternative to SSI, it is evaluated by CDN. ESI has to have some proxy that is able to process its tags and often made available as part of CDN.

  • JavaScript – Ajax

Using JSI will replace dynamic components with ajax tags, so they are loaded by the browser. If included component has some JS code, it may not work properly, as it won’t be initialized immediately after a page is loaded.

  • Add comment – adds debug comment: <!– SDI include (path: %s, resourceType: %s) –> to every replaced component.
  • Filter selector – selector added to HTTP request for particular component and is used to get actual content.
  • TTL – time to live in seconds, set for rendered component. This property is supported for dispatcher version 4.1.11+
  • Required header – SDI will be enabled only if the configured header is present in the request. By default it’s Server-Agent=Communique-Dispatcher header, added by the AEM dispatcher. You may enter just the header name only or the name and the value split with =.
  • Ignore URL params – SDI normally skips requests containing any GET parameters. This option allows to set a list of parameters that should be ignored.
  • Include path rewriting — enable rewriting link (according to sling mappings) that is used for dynamic content including.

 

Step 3:

 

After completion of Step 1 and Step 2 on publishing instance, Dispatcher configurations need to be updated as explained below:

1. Include(If already present, make sure uncommented) the mod_include module in Apache Web server’s httpd.conf file:

LoadModule include_module modules/mod_include.so

2. Update virtual host configuration file

a. Find the following lines in the dispatcher.conf file

<IfModule dispatcher_module>
	SetHandler dispatcher-handler
</IfModule>

modify as below

<IfModule dispatcher_module>
	SetHandler dispatcher-handler
</IfModule>
SetOutputFilter INCLUDES

b. Add Includes to Options directive:

<VirtualHost *:80>
...
<Directory />
				...
Options FollowSymLinks Includes  
AllowOverride None
...
		<Directory>
		...
</VirtualHost>

3.  Update the httpd.conf to enable SDI.

a. Add “Includes” to Options directive to enable SSI includes used by Sling Dynamic Include

b. Specify what file types are to be processed by Includes filter.

   <Directory /mnt/var/www/html>	
      ...
      Options Indexes FollowSymLinks Includes  
	... 
      AddOutputFilter INCLUDES .html 
	AddOutputFilterByType INCLUDES text/plain text/html
      ...
   </Directory>

4. Update rules.any or dispatcher.any depending where the cache rules are defined for the publish instance.

/0008 {
    		/glob "*.nocache.html*"
    		/type "deny"
  	}

Make sure the selector ‘nocache’ used here is same as defined in OSGI config (include-filter.config.selector = ‘nocache’) explained in Step 2.

5. Restart the server using any of the below commands:

sudo apachectl restart OR sudo service httpd restart

 

Verification

After setting up the SDI, it’s time to verify the changes. Follow the below steps:

 

  1. Right click and open the page source on the webpage where the component is dynamically included.
  2. In the page source, search for SDI includes tag.
  3. A component configured for SDI will be replaced with SDI tags as shown below:

Share it:

Ankur Mittal

January 21, 2019

Leave a Comment

Related Posts