2

I am facing an issue getting an environment variable value from Sitecore deployment app in my Next JS application. I've set an environment variable XMC_DEFAULT_RH in my XM Cloud instance Deployment app and I'm trying to read this XM Cloud environment variable in the Next JS application.

Here is the relevant line from my next.config.js:

code:

assetPrefix: process.env.XMC_DEFAULT_RH === 'true' ? publicUrl : undefined,

I have deployed this change on both XM Cloud as well as vercel

The expected behavior is for the assetPrefix to use publicUrl when XMC_DEFAULT_RH is set to 'true' in XM Cloud environment variable. However, it's returning undefined, indicating that it is not recognizing the variable value.

Here's what I've confirmed so far: The environment variable is set correctly in Sitecore XM Cloud Environment variable(name and value). There are no typos in the environment variable name.

enter image description here

Any advice or insights would be greatly appreciated!

Thank you.

9
  • How are you fetching 'publicUrl' ? Is it possible that it is undefined ? Commented Apr 26 at 10:39
  • Additionally, you can also try - process.env.XMC_DEFAULT_RH == 'true'. (===) will strictly compare data type and value. (==) will loosely compare so if XMC_DEFAULT_RH is 1, it will result true. Commented Apr 26 at 10:46
  • 1
    Are you setting the env variable in the XM Cloud Deploy app and expecting that to exist in your Vercel deployment? They are separate systems.
    – Richard Seal
    Commented Apr 26 at 16:07
  • 1
    After you created the XMC_DEFAULT_RH variable and modified the config you have to redeploy both Sitecore and Vercel apps to apply your change. Commented Apr 28 at 12:44
  • 1
    @AtulGusain - Do you want to read the variable in vercel end? If yes, you need to create the variable in vercel (vercel.com/your-organisation/your-application/settings/…) Or do you want to read the variable in Sitecore application(Pages.siteorecloud.io). The above settings will work in XMCloud apps only and not in the vercel apps Commented Apr 29 at 7:02

1 Answer 1

1

You have a few errors in the setup here:

  1. For an environment variable to be used on your editing host, you must create it in the deploy app and set the Target to Rendering Host. That will make the variable available to your Next.js application when running on your editing host in XM Cloud only
  2. For the variable to be used on your Next.js application that is deployed to Vercel, you must create that environment variable in your Vercel account and set the environment value to match (Production, Preview etc..). (Documentation on creating environment variables in Vercel is here: https://vercel.com/docs/projects/environment-variables).

Once you have created these variables, you will need to redeploy your application otherwise the values will not be available to your application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.