Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Loading content based on domain

s1101v
Level 3
Level 3

Hello Community - I am setting up the siteId based on the domain and would like to load different content specific to each domain.

<sly data-sly-test="${!wcmmode.edit}">
<script type="text/javascript">
if (location.hostname.indexOf("company") > -1) {
var siteId='company';
}
if (location.hostname.indexOf("aemcommunity") > -1) {
var siteId='aemcommunity';
}
</script>
</sly>

 

If it is a sightly expression, this can be used in data-sly-test but this is a javascript variable, how can I use it based on the condition. Could someone suggest on this?


<sly data-sly-test="">   -------> siteId='company'
<div>content loaded from company domain</div>
</sly>

<sly data-sly-test="">  ------->  siteId='aemcommunity'
<div>content loaded from aemcommunity domain</div>
</sly>

1 Accepted Solution
Arun_Patidar
Correct answer by
Community Advisor
Community Advisor

Hi, In this case, setup value using context-aware configuration and read it in sightly using caconfig global object.

you may need to enable read access for everyone group for /conf folder but direct access to /cont can be denied from dispatcher.

https://sling.apache.org/documentation/bundles/context-aware-configuration/context-aware-configurati...

View solution in original post

3 Replies
Shashi_Mulugu
Community Advisor
Community Advisor

@s1101v you can't simply do it in sightly as in AEM content/page will be cached in general cases. 

From the usecase you are more likely looking for a dynamic component. 

 

You will have couple of options:

1. Use Sling Dynamic Include option to load that specific component Dynamically everytime.

 

2. Use Javascript to decide the domain and make an Ajax call to get the domain specific content maybe to a content fragments.

 

FYI.  

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/dynamic-loading-of-geotarg...

Jörg_Hoh
Employee
Employee

As already mentioned by Shashi Mulugu you have to consider that the page is cached. You should make any assumption on rendering about the domain name via which the page is requested. Instead you can derive from the location of the resources you are rendering in which context this rendering operation happens.

That means, that in general you should reflect this difference in behavior into the resource tree, so you don't need to make any hardcode any decision, but instead make it depend on a property in a component or page. Then you can allow the business user (author) to change that behavior. In more complex cases you could implement inheritance of such a setting.

 

But hardcoding behavior based on domain names is a bad design. Because at that point you should think of not only production environments, but also many test environments.

 
Arun_Patidar
Correct answer by
Community Advisor
Community Advisor

Hi, In this case, setup value using context-aware configuration and read it in sightly using caconfig global object.

you may need to enable read access for everyone group for /conf folder but direct access to /cont can be denied from dispatcher.

https://sling.apache.org/documentation/bundles/context-aware-configuration/context-aware-configurati...

View solution in original post