Expand my Community achievements bar.

SOLVED

Runmode library not working in AEM cloud

Avatar

Level 3

Hi,

I tried to check the runmode in AEM in order to add the script accordingly to PROD and non-PROD environments. When I try to execute the code both the script is displaying in the PROD and non-PROD environment. Below is the code added:

 

Runmode.Java : 

import com.adobe.acs.commons.util.ModeUtil;

public boolean isProduction() {
return ModeUtil.isRunmode("prd");
}

 

 

component.html :

<sly data-sly-use.rumode="com.adobe.aem.test.models.Runmode">
<sly data-sly-test="${rumode.production}"></sly>
        <script src="https://test.com/test-prod.js"></script>
</sly>      
<sly data-sly-test="${!rumode.production}"></sly>
        <script src="https://test.com/test-stage.js"></script>
</sly>
</sly>
1 Accepted Solution

Avatar

Correct answer by
Community Advisor
7 Replies

Avatar

Community Advisor

Hi @nivethaS
Please try to check your code with below piece of code. The run mode you are passing in function isProduction should contain correct information, try to debug you java code and see what info. you are getting.

 

 

public static WCMMode getMode(SlingHttpServletRequest req) {
        if (req.getAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME) == null) {
            return WCMMode.DISABLED;
        } else {
            String mode = String.valueOf(req.getAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME));
            try {
                return WCMMode.valueOf(mode);
            } catch (IllegalArgumentException ex) {
                return WCMMode.DISABLED;
            }
        }
    }



 public static boolean isRunmode(String mode) {
        return runmodes.contains(mode);
    }


Thanks
Tarun

Avatar

Level 3

Hi @TarunKumar 

I need to check whether the environment is prod or non prod but the codded you shared is to validate the wcmmode right?

Avatar

Community Advisor

Hi Nivetha,

 

You are checking for a specific run mode called 'prd' in code supplied,

 

Kindly ensure that your instance actually contains or does no contain this specific run mode.

 

Follow following link to read about setting specific run modes:

https://experienceleague.adobe.com/docs/experience-manager-65/content/implementing/deploying/configu...

 

Regards,

Peter

Avatar

Correct answer by
Community Advisor

Avatar

Administrator

@nivethaS Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni

Avatar

Community Advisor

Hi @nivethaS 

 

In AEM Cloud, runmode detection using com.adobe.acs.commons.util.ModeUtil isn't supported. AEM Cloud utilizes environment variables instead of traditional runmodes like "prod" or "stage".

 

Therefore, you need to adapt your code to use these environment variables:

 

1. In Runmode.Java:

Replace your existing logic with:

Java
 
public boolean isProduction() {
  return System.getenv("ENVIRONMENT_TYPE").equals("production");
}
 

This retrieves the ENVIRONMENT_TYPE environment variable and checks if it's equal to "production".

 

2. In component.html:

Update your data-sly-test syntax and use the environment variable directly:

HTML
 
<sly data-sly-test="${System.getenv('ENVIRONMENT_TYPE') eq 'production'}">
  </sly>

<sly data-sly-test="${System.getenv('ENVIRONMENT_TYPE') ne 'production'}">
  </sly>
Use code with caution.

This checks the ENVIRONMENT_TYPE value against "production" using the equality and inequality operators (eq and ne).

 

By changing your approach to leveraging environment variables, you'll achieve proper runmode-based script loading in AEM Cloud.

 

Remember to clear the AEM Cloud dispatcher and browser cache after making these changes to ensure you're seeing the updated behavior.

Avatar

Administrator

@nivethaS Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni