This question is related to an Integration Scenario between another SAP product and Spartacus library.
Suppose we have an existing Angular application (let's call it Portal Dashborad Application) which already works quite well for years. Now the new enhancement requirement is: by pressing a hyperlink, it will navigate to a new page called Product Catalog Page. In Product catalog page, the product data is fetched from Commerce Cloud via OCC API.
Currently, a feasibility check is on-going, to measure whethe the Product Catalog Page could be implemented by leveraging Spartacus library.
Requirement of Product Catalog Page development
the source code of it should eventually be put into a seperate, dedicated github repository, and imported into Portal Dashboard Application as a NPM package.
it should try to use Spartacus library.
Current POC
This is the current POC as the first step to try to include Spartacus into Portal Dashboard Application.
while CatalogModule itself is lazy-loaded by DashboardRoutingModule as a feature module, so in this case, per my understanding, Spartacus module is also lazy loaded.
Current POC Problem
Simply clone the repo, yarn install and yarn start to launch it.
Open url http://localhost:4200/catalog,which will trigger loading of CatalogModule. The loading will fail and the following error message is shown:
core.js:6498 ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(CatalogModule)[UserAuthEventModule -> UserAuthEventBuilder -> UserAuthEventBuilder -> AuthService -> OAuthLibWrapperService -> OAuthService -> OAuthService -> OAuthService]:
NullInjectorError: No provider for OAuthService!
NullInjectorError: R3InjectorError(CatalogModule)[UserAuthEventModule -> UserAuthEventBuilder -> UserAuthEventBuilder -> AuthService -> OAuthLibWrapperService -> OAuthService -> OAuthService -> OAuthService]:
NullInjectorError: No provider for OAuthService!
The mentioned OAuthService belongs to a Spartacus dependency angular-oauth2-oidc.
We can mitigate this issue by adding OAuthModule.forRoot() in DashboardModule:
Afterwards the error message changes:
If we directly put SpartacusModule to DashboardModule's imports array,all the injection errors will disappear. Unfortunately, this will break the lazy loading mechanism.
When we access http://localhost:4200,/ SpartacusModule gets loaded, we can observe the url changes to http://localhost:4200/electronics-spa/en/USD/ automatically,the first OCC request for base site has fired.
This eager load behavior is NOT what we want.
Question
If Spartacus library is installed via Schematics, by default it will be eager-loaded into AppModule's imports array as demonstrated below:
Do we have solutions / workaround to have SpartacusModule lazy-loaded as a feature module?
it's confirmed: The SpartacusModule is NOT designed to be lazy loaded. Only the individual feature libraries in the feature-libs/ folder of the repo are designed to be lazy loaded.
The background of this question
This question is related to an
Integration Scenario
between another SAP product and Spartacus library.Suppose we have an existing Angular application (let's call it
Portal Dashborad Application
) which already works quite well for years. Now the new enhancement requirement is: by pressing a hyperlink, it will navigate to a new page calledProduct Catalog Page
. In Product catalog page, the product data is fetched from Commerce Cloud via OCC API.Currently, a feasibility check is on-going, to measure whethe the Product Catalog Page could be implemented by leveraging Spartacus library.
Requirement of Product Catalog Page development
eventually
be put into a seperate, dedicated github repository, and imported into Portal Dashboard Application as a NPM package.Current POC
This is the current POC as the first step to try to include Spartacus into Portal Dashboard Application.
Spartacus module is imported into CatalogModule,
while CatalogModule itself is lazy-loaded by DashboardRoutingModule as a feature module, so in this case, per my understanding, Spartacus module is also
lazy loaded
.Current POC Problem
Simply clone the repo,
yarn install
andyarn start
to launch it.Open url
http://localhost:4200/catalog
,which will trigger loading ofCatalogModule
. The loading will fail and the following error message is shown:The mentioned OAuthService belongs to a Spartacus dependency
angular-oauth2-oidc
.We can mitigate this issue by adding
OAuthModule.forRoot()
in DashboardModule:Afterwards the error message changes:
If we directly put SpartacusModule to
DashboardModule
'simports
array,all the injection errors will disappear. Unfortunately, this will break the lazy loading mechanism.When we access http://localhost:4200,/ SpartacusModule gets loaded, we can observe the url changes to
http://localhost:4200/electronics-spa/en/USD/
automatically,the first OCC request for base site has fired.This
eager load
behavior is NOT what we want.Question
If Spartacus library is installed via Schematics, by default it will be
eager-loaded
into AppModule'simports
array as demonstrated below:Do we have solutions / workaround to have SpartacusModule lazy-loaded as a feature module?