Search code examples
angularspartacus-storefrontcommercesap-commerce-cloud

Spartacus at Commerce Cloud is making requests to the base url localhost:9002


I am setting up a Commerce Cloud environment at SAP Commerce Cloud V2. And I am having trouble with occBaseUrl.

I have 4 environment files (environment.ts, environment.qa1.ts, environment.prod.ts and environment.preprod.ts). In three of them, I am exporting constant environment.occBaseUrl as undefined. In environment.ts, I am exporting it as localhost:9002.

I inserted the meta tag

  <meta name="occ-backend-base-url" content="OCC_BACKEND_BASE_URL_VALUE" />

at index.html.

And, my D1 environment at SAP Commerce Cloud is requesting to baseUrl localhost:9002. So, I am out of images at my Spartacus storefront.

I didn't find many questions for this problem. I don't know if there is. But I read the documentation and didn't understand.


Solution

  • Unfortunately this area depends both on which Spartacus version you are currently using, plus your setup in package.json / angular.json as that drives the build.

    In Commerce Cloud v2, the builder only runs once to produce a runnable build & that picks up the environment.ts file dictated by your package.json/angular.json settings - that build is intended to then be used across your different environments. The index.html meta tag substitution happens independently during startup of the JS Storefront server, so that is picks up the API endpoint specific to the environment it is running in (DA / S1 / etc).

    Really the intention (as far as I understand it) is to have the prod settings included in the build ... but that depends on either having the package.json "script", "build" ultimately using ng build --prod, or in angular.json having "build", "defaultConfiguration" set to "production". The Commerce Cloud environment will follow whatever is set in your files (it just does "yarn build").

    There are some crucial differences in the Spartacus versions however. Before Spartacus 4, the default setup does not build the prod environment by default & you would need to amend the files mentioned above. From Spartacus 4 it has "defaultConfiguration" set to "production". Another change from Spartacus 4 onwards is that the meta tag substitution overrides the environment file setting ... in Spartacus 3 and before it is the other way around.

    This help page references some sample setup repos: https://help.sap.com/viewer/1be46286b36a4aa48205be5a96240672/v2105/en-US/76450bc02bdf492689ca5e6d35c670e6.html - you need to look at the branches related to Spartacus (2105-spartacus, 2005-spartacus, etc). Note that these are currently for Spartacus v4 & if you need to see the setup for an older version you will need to look at the historical commits.