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
744 stars 389 forks source link

Is Spartacus module NOT supported to lazy load as a Feature Module? #16093

Closed wangzixi-diablo closed 2 years ago

wangzixi-diablo commented 2 years ago

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 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

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

clipboard1

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.

clipboard2

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!

clipboard3

The mentioned OAuthService belongs to a Spartacus dependency angular-oauth2-oidc.

We can mitigate this issue by adding OAuthModule.forRoot() in DashboardModule

clipboard4

Afterwards the error message changes:

clipboard5

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.

clipboard6

Question

If Spartacus library is installed via Schematics, by default it will be eager-loaded into AppModule's imports array as demonstrated below:

image

Do we have solutions / workaround to have SpartacusModule lazy-loaded as a feature module?

wangzixi-diablo commented 2 years ago

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.