Loading content based on domain

Avatar

Avatar

s1101v

Avatar

s1101v

s1101v

05-03-2021

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>

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

3.1K

Likes

1.2K

Correct Reply

883

Avatar

Arun_Patidar

MVP

Total Posts

3.1K

Likes

1.2K

Correct Reply

883
Arun_Patidar
MVP

07-03-2021

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...

Answers (2)

Answers (2)

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.1K

Likes

1.1K

Correct Reply

1.1K

Avatar

Jörg_Hoh

Employee

Total Posts

3.1K

Likes

1.1K

Correct Reply

1.1K
Jörg_Hoh
Employee

07-03-2021

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.

 

Avatar

Avatar

Shashi_Mulugu

MVP

Avatar

Shashi_Mulugu

MVP

Shashi_Mulugu
MVP

05-03-2021

@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...