Loading content based on domain

Avatar

Avatar
Validate 1
Level 1
s1101v
Level 1

Likes

0 likes

Total Posts

26 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Give Back
View profile

Avatar
Validate 1
Level 1
s1101v
Level 1

Likes

0 likes

Total Posts

26 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Give Back
View profile
s1101v
Level 1

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
Coach
MVP
Arun_Patidar
MVP

Likes

1,462 likes

Total Posts

3,329 posts

Correct reply

949 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,462 likes

Total Posts

3,329 posts

Correct reply

949 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
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
Coach
Employee
Jörg_Hoh
Employee

Likes

1,141 likes

Total Posts

3,167 posts

Correct reply

1,083 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,141 likes

Total Posts

3,167 posts

Correct reply

1,083 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
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
Springboard
MVP
Shashi_Mulugu
MVP

Likes

234 likes

Total Posts

295 posts

Correct reply

67 solutions
Top badges earned
Springboard
Bedrock
Validate 1
Applaud 100
Establish
View profile

Avatar
Springboard
MVP
Shashi_Mulugu
MVP

Likes

234 likes

Total Posts

295 posts

Correct reply

67 solutions
Top badges earned
Springboard
Bedrock
Validate 1
Applaud 100
Establish
View profile
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...