SAP / spartacus

Spartacus is a lean, Angular-based JavaScript storefront for SAP Commerce Cloud that communicates exclusively through the Commerce REST API.
Apache License 2.0
738 stars 383 forks source link

[decision needed] Prioritise meta injected baseUrl over static configuration #5886

Open tobi-or-not-tobi opened 4 years ago

tobi-or-not-tobi commented 4 years ago

As long as we do not have #5772 in place, developers suffer from not having a single source for a local build and ccv2 build. This is caused by the fact that for local development, the OCC baseUrl is configured directly in the static configuration, where as a ccv2 deployment can benefit from an injected baseUrl (#1148). Since an environment specific build is not automated, developers cannot use environment specific configurations.

An easy fix for this is to change the priority: load the OCC baseUrl from static configuration unless there's a configuration driven from the injected meta tag.

Alternatively we could introduce a new feature for local developers: use a local storage key for the OCC baseUrl. This local storage key/value will take precedence (only in dev mode), so that a configuration is not even needed in the code base.

tobi-or-not-tobi commented 4 years ago

@neilhubertprice @mpern

tobi-or-not-tobi commented 4 years ago

Workaround for local development: Add an optional configuration, driven by environment properties (see https://angular.io/guide/build):

export const environment = {
  production: false,
  occBaseUrl:
    'https://storefront.c39j2-walkersde1-d4-public.model-t.cc.commerce.ondemand.com',
};

Configure the app.module with the environment variable:

B2cStorefrontModule.withConfig({
  backend: {
    occ: {
      baseUrl: environment.occBaseUrl,
      legacy: false,
    }
  }
  [...]
}

When the production build is done in ccv2, the environment.prod.ts file will be used. As long as the occBaseUrl is not defined, it will fallback to the meta data property.

TDawg225 commented 4 years ago

As long as the occBaseUrl is not defined, it will fallback to the meta data property. This doesn't work in practice because the build will fail when baseUrl is missing in environments.prod.ts. I think the best workaround is to set occBaseURL to localhost:9002 in environment.ts and then hardcode the API aspect endpoint in environment.prod.ts, since Commerce Cloud uses ng serve --prod, regardless of environment.

neilhubertprice commented 4 years ago

You should not hardcode the occBaseURL, it changes between environments & will get injected automatically via the meta data in Commerce Cloud

tobi-or-not-tobi commented 4 years ago

@TDawg225 you should have the occBaseUrl defined in the environment.prod.ts, but keep it empty.

artem-zur commented 4 years ago

Hi guys! Any updates on it ?

We also have a typo in https://sap.github.io/spartacus-docs/configuring-base-url/. “This value is overridden by the value from the meta tag.” != “Note: The value of the backend.occ.baseUrl from the withConfig method takes precedence over the value from the meta tag.“.

P.S. I'm really like the idea with changing the priority to meta tag, cuz it will work fine for all environments (incl. Cloud CCv2) and especially for local development but without additional tricky configuration.

Xymmer commented 4 years ago

to discuss with Tobias for prioritization

mpern commented 3 years ago

What's the status of this bug?

this is such a common stumbling block for newcomers deploying Spartacus on CCV2. Please prioritize the meta tag over all other configuration.

Xymmer commented 3 years ago

@mpern thanks for your feedback. our architecture team will re-review and decide on our course of action. we agree it's important.

bechte commented 3 years ago

Is there an update on this issue? Version 4 has been released, but no update yet. Thanks.

mpern commented 3 years ago

I just retested it with Spartacus 4.0.1: Issue resolved.

The occ-backend-base-url tag overrides any baseUrl configuration coming from the source code.

fabiofilpi commented 2 years ago

I'm having trouble with this topic. I saw the documentation saying to use But, then, how can I call the base occ url?