April 26, 2020
Estimated Post Reading Time ~

Sling Dynamic Include

The purpose of the sling dynamic includes (SDI) is to replace dynamically generated components (eg. current time or foreign exchange rates) with server-side include tag (eg. SSI or ESI). Therefore the dispatcher is able to cache the whole page but dynamic components are generated and included with every request. Components to include are chosen in filter configuration using the resourceType attribute.

When the filter intercepts request for a component with given resourceType, it'll return a server-side include tag (eg. <!--#include virtual="/path/to/resource" --> for Apache server). However, the path is extended by a new selector (nocache by default). This is required because the filter has to know when to return actual content.
Components don't have to be modified in order to use this module (or even aware of its existence). It's servlet filter, installed as an OSGi bundle and it can be enabled, disabled, or reconfigured without touching AEM installation.

Let's try ☺
Let's do this practically by loading a component with and without SDI.


Component without SDI:
Load the page without configuring SDI and check the view source.


Configure SDI:
Let's configure the SDI from ConfigMgr.
SDI Configuration

The filter is delivered as a standard OSGi bundle. SDI is configured via the configuration factory called SDI Configuration.

In the above screenshot, I have enabled SDI and entered the base path and the component to be included using SDI. I have selected include type as Javascript. So in view of the source we can observe that component is replaced with Javascript code.

Following properties are available:
  • Enabled - enable SDI
  • Base path - given SDI configuration will be enabled only for this path
  • Resource types - which components should be replaced with tags
  • Include type - a type of include tag (Apache SSI, ESI or Javascript)
  • Add comment - adds debug comment: <!-- SDI include (path: %s, resourceType: %s) --> to every replaced component
  • Filter selector - selector used to get the actual content
  • Component TTL - time to live in seconds, set for rendered component (require Dispatcher 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 all requests containing any GET parameters. This option allows setting a list of parameters that should be ignored in the test. See the Ignoring URL parameters section in the dispatcher documentation.
  • Include path rewriting - enable rewriting link (according to sling mappings) that is used for dynamic content including.
Component with SDI (Javascript):
Now configure the SDI include type as "Javascript". Reload the page and go to view source.

In view source, we can observe that SDI script is included.

<!-- SDI include (path: /content/sightly-test/_jcr_content/par/langcomp.nocache.html, 

resourceType: sightlyProject/components/content/langComp) -->
<div id="dynamic_include_filter_div_1007"></div>
<script type="text/javascript">
jQuery.get("\/content\/sightly-test\/_jcr_content\/par\/langcomp.nocache.html", 
 function(data) {
  jQuery("#dynamic_include_filter_div_1007").replaceWith(data);
 });
</script>

Component loaded with SDI (Javascript)

Component with SDI (Apache SSI):
Now configure the SDI include type as "Apache SSI". Reload the page and go to view source.
In view source, we can observe that SDI script is included.

<!-- SDI include (path: /content/sightly-test/_jcr_content/par/langcomp.nocache.html, 
resourceType: sightlyProject/components/content/langComp) -->
<!--#include virtual="/content/sightly-test/_jcr_content/par/langcomp.nocache.html" -->

Component loaded with SDI (Apache SSI)

How to test sling dynamic include:
Lets test sling dynamic include with Apache SSI. In order to test this 
  • first configure dispatcher, if not configured see here to configure dispatcher.
  • Enable SSI in Apache & dispatcher, click here to see the configs.
  • Now bring up your dispatcher.
Dispatcher is up and running

Testing without SDI

Testing with SDI (Apache SSI)

Benefits:
As SDI components are loaded at runtime, remaining page is loaded from dispatcher cache page will load faster.

Load time without SDI

Load time with SDI (Apache SSI)

Troubleshooting SDI:
If SDI is not working, then check the below things:
  • Check if all the configurations are done correclty.
  • In configMgr, check if resource type is not appended with either /apps/ or /libs/.
References:
SDI demo by Tomasz Rękawek, Cognifide
SDI Presentation
SDI - Cognified blog
Official SDI extension
GitHub Code


By aem4beginner

No comments:

Post a Comment

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