Expand my Community achievements bar.

Join expert-led, customer-led sessions on Adobe Experience Manager Assets on August 20th at our Skill Exchange.
SOLVED

deploy a clientlib or component

Avatar

Level 2

How do you conditionally deploy a clientlib or component only in the AEM Stage or Prod environment in AEM as a Cloud Service - without using runmodes, which are no longer supported in the traditional sense?

Most documentation still refers to classic AEM paradigms. Curious to know how others have approached this in AEM Cloud-native setups.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

in Stage and PROD you cannot do that, as the same immutable content image is used for both. That means you need some runtime logic which achieves that. 

 

(My question would be why this is even necessary ... I could imagine some cases, but have barely seen any really convincing case ...)

View solution in original post

4 Replies

Avatar

Community Advisor

Hi @EthanRo5,

Since traditional runmode-based includes (like clientlibs.<runmode>.js) or Sling runmode folders (e.g., config.author, config.publish, etc.) aren't fully supported in AEMaaCS due to its immutable runtime model.

However, you can still achiever this:

Use AEM Environment Variables and Context-Aware Configurations with Conditional Logic in HTL or JavaScript.

1. Define the Environment Type Using OSGi Config or Metadata

Use the Environment Indicator service or create your own service that reads the environment type from:

  • com.adobe.granite.cloud.env

  • Or inject via AEM Environment Variable (Adobe I/O > Cloud Manager > Environment Variables)

Example OSGi config value via context-aware:

 
@Designate(ocd = MyEnvConfig.class)
@Component(service = MyEnvService.class)
public class MyEnvServiceImpl implements MyEnvService {
    @Activate
    public void activate(MyEnvConfig config) {
        this.environment = config.environmentType(); // dev/stage/prod
    }
}
2. Expose Environment to HTL Using Sling Model
@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class EnvModel {
    @OSGiService
    private MyEnvService myEnvService;

    public String getEnvType() {
        return myEnvService.getEnvironment(); // e.g., "stage"
    }
}
3. Use it in HTL to Load ClientLib Conditionally

<sly data-sly-use.env="com.myproject.models.EnvModel" />
<sly data-sly-test="${env.envType == 'stage' || env.envType == 'prod'}">
  <cq:includeClientLib categories="my-secure-clientlib"/>
</sly>

I hope this will work.


Santosh Sai

AEM BlogsLinkedIn


Avatar

Level 4

Hi @EthanRo5 ,

Use environment variables via cloud manager

Set ENV variable in Cloud Manager pipeline:

Example: ENV_TYPE=stage

Access in Java code or Sling Model:

 

String env = System.getenv("ENV_TYPE");

 

Use in HTL to conditionally load clientlib:

 

<sly data-sly-test="${env == 'prod'}">
  <script src="/etc.clientlibs/site/clientlibs/prod-only.js"></script>
</sly>

 

Hope this helpful.

 

Regards,

Karishma.

Avatar

Correct answer by
Employee Advisor

in Stage and PROD you cannot do that, as the same immutable content image is used for both. That means you need some runtime logic which achieves that. 

 

(My question would be why this is even necessary ... I could imagine some cases, but have barely seen any really convincing case ...)

Avatar

Administrator

@EthanRo5 Just checking in — were you able to resolve your issue?
We’d love to hear how things worked out. If the suggestions above helped, marking a response as correct can guide others with similar questions. And if you found another solution, feel free to share it — your insights could really benefit the community. Thanks again for being part of the conversation!



Kautuk Sahni