Updating Spartacus to Version 4.0
Note: Spartacus 4.x is no longer maintained. Please upgrade to the latest version.
Note: Spartacus 4.x was tested with SAP Commerce Cloud versions 1905 to 2205. Spartacus 4.x has not been verified to work with (and is not guaranteed to work with) SAP Commerce Cloud 2211 or later releases.
Before you migrate to version 4.0 of the Spartacus libraries, it is highly recommend that you update your app structure to match the Spartacus reference app structure, and that you also make the move to using feature libraries. It is easier to perform the migration in multiple, small steps (migrating to the new app structure, switching to the extracted feature libraries, and then migrating to 4.0), where you can make sure that everything still works as before after each step.
Note: The procedures below are listed in the order they should be performed, which is to say, migrate to the reference app structure first, then upgrade your Angular libraries, then upgrade to Spartacus 3.4.x, and finally, upgrade to Spartacus 4.0.
Table of Contents
- Overview
- Migrating to the Reference App Structure
- Upgrading Your Angular Libraries
- Upgrading Spartacus to 3.4.x
- Upgrading Spartacus to 4.0
- Migrating from Accelerator to Spartacus
Overview
Before the 3.0 release, Spartacus started to have separate libraries based on their responsibility. With 3.0, Spartacus was released with a few libraries already in separate packages (such as @spartacus/organization and @spartacus/storefinder). More libraries continued to be moved in the minor 3.x releases. This was done while trying to avoid breaking changes. However, each major release provides an opportunity to pay off technical debt that has accumulated during the minor releases. The extracted libraries are a big contributor to technical debt, because the same functionality has been kept in two places. With the 4.0 release, the functionality that was extracted to separate libraries in the minor releases is now removed from the core libraries (that is, from @spartacus/core, @spartacus/storefront, @spartacus/assets and @spartacus/styles).
To accommodate these changes, it was necessary to also modify a few of the bigger modules, namely B2cStoreFrontModule, StorefrontModule and CmsLibModule.
For these reasons, it is strongly recommended to switch to the new app structure, which does not use these modules, and to switch to the new feature libraries if they exist for the features you are using.
For more information on the Spartacus reference app structure, see Reference App Structure.
Migrating to the Reference App Structure
The following steps describe how to migrate to the new reference app structure.
- Create a module called
SpartacusModulewith the following path:app/spartacus/spartacus.module.ts. - Add the
SpartacusModuleto theimportsinAppModule. - Add
BaseStorefrontModuleto the imports and exports of theSpartacusModule. This module is exported from the@spartacus/storefrontlibrary. - Create a module called
SpartacusFeaturesModulewith the following path:app/spartacus/spartacus-features.module.ts - Add the
SpartacusFeaturesModuleto theimportsinSpartacusModule. - Create a module called
SpartacusConfigurationModulewith the following path:app/spartacus/spartacus-configuration.module.ts. - Add the
SpartacusConfigurationModuleto theimportsin theSpartacusModule. -
Move Spartacus configurations to the
SpartacusConfigurationModule.These are the configurations you pass with
provideConfig,provideConfigFactory, or with thewithConfigmethods from some of the modules (such as theB2cStorefrontModuleandConfigModule). It is recommended that you useprovideConfigorprovideConfigFactoryin module providers to configure Spartacus. -
Configure the
AppRoutingModule.If you do not have this module, first create it under
app/app-routing.module.ts. Make sure you import this module in youAppModule. In the imports ofAppRoutingModule, configure theRouterModulewith the following options:RouterModule.forRoot([], { anchorScrolling: 'enabled', relativeLinkResolution: 'corrected', initialNavigation: 'enabled', }),Previously, this module was configured in
StorefrontModule, which is now deprecated and removed in version 4.0. -
Configure the NgRx modules in your
AppModule.The NgRx modules were part of the
StorefrontModule, but similarly to theRouterModule, Spartacus requires this configuration to be present in the application. You need to add the following modules to theimportsof yourAppModule:StoreModule.forRoot({})andEffectsModule.forRoot([]). Import these modules from@ngrx/storeand from@ngrx/effects, respectively. The following is an example:@NgModule({ imports: [ AppRoutingModule, StoreModule.forRoot({}), EffectsModule.forRoot([]), SpartacusModule, // then the rest of your custom modules... ], ... - Continue with the following procedures, which focus on replacing deprecated Spartacus grouping modules.
Migrating the B2cStorefrontModule
- From the steps in the previous section, the configuration from
B2cStorefrontModule.withConfigshould already be moved to theSpartacusConfigurationModuleand provided withprovideConfig, but if not, then you can do so now. - Add the
HttpClientModuleto the imports in yourAppModuleif it is not there. -
In the
SpartacusFeaturesModulemodule imports, add theStorefrontModuleandCmsLibModulefrom the@spartacus/storefrontlibrary.These modules are removed in 4.0, but it is better to migrate the modules step by step. The migration of these modules is covered in the next steps.
-
The
B2cStorefrontModuleprovided a few default configurations. If you rely on them, add them to yourSpartacusConfigurationModule. The following is an example:provideConfig({ pwa: { enabled: true, addToHomeScreen: true, }, }), provideConfig(layoutConfig), provideConfig(mediaConfig), ...defaultCmsContentProviders, - Remove any use of the
B2cStorefrontModulefrom your app.
Migrating the B2bStorefrontModule
- From the steps in the first section, the configuration from
B2bStorefrontModule.withConfigshould already be moved to theSpartacusConfigurationModuleand provided withprovideConfig, but if not, then you can do so now. - Add the
HttpClientModuleto the imports in yourAppModuleif it is not there. -
In the
SpartacusFeaturesModulemodule imports, add theStorefrontModuleandCmsLibModulefrom the@spartacus/storefrontlibrary.These modules are removed in 4.0, but it is better to migrate the modules step by step. The migration of these modules is covered in the next steps.
- In the
SpartacusFeaturesModulemodule imports, addCostCenterModule.forRoot()from the@spartacus/corelibrary. -
The
B2bStorefrontModuleprovided a few default configurations. If you rely on them, add them to yourSpartacusConfigurationModule. The following is an example:provideConfig(layoutConfig), provideConfig(mediaConfig), provideConfig(defaultB2bOccConfig), provideConfig(defaultB2bCheckoutConfig), ...defaultCmsContentProviders, - Remove any use of the
B2bStorefrontModulefrom your app.
Migrating the StorefrontModule
- From the steps in the first section, you should have already configured
RouterModuleinAppRoutingModule, but if not, then you can do so now. - The
importsof yourAppModuleshould already includeStoreModule.forRoot({})andEffectsModule.forRoot([]), but if not, you can add them now. - In the
SpartacusFeaturesModule, import theAsmModulefrom@spartacus/storefront. - Add the
StorefrontFoundationModuleto the imports of theSpartacusFeaturesModule. - Add the
MainModuleto the imports of theSpartacusFeaturesModule. - In the
SpartacusFeaturesModule, importSmartEditModule.forRoot(),PersonalizationModule.forRoot()andOccModule.forRoot()from@spartacus/core. - In the
SpartacusFeaturesModule, import theProductDetailsPageModuleandProductListingPageModulefrom@spartacus/storefront. - In the
SpartacusFeaturesModule, importExternalRoutesModule.forRoot()from@spartacus/core. - Remove any use of the
StorefrontModulefrom you app.
Migrating the CmsLibModule
-
In the
SpartacusFeaturesModule, import the following modules from@spartacus/storefront:AnonymousConsentManagementBannerModule, AsmModule, // remove if it's already present HamburgerMenuModule, CmsParagraphModule, LinkModule, BannerModule, CategoryNavigationModule, NavigationModule, FooterNavigationModule, BreadcrumbModule, SearchBoxModule, SiteContextSelectorModule, QualtricsModule, AddressBookModule, OrderHistoryModule, OrderCancellationModule, OrderReturnModule, ReturnRequestListModule, ReturnRequestDetailModule, ProductListModule, ProductFacetNavigationModule, ProductTabsModule, ProductCarouselModule, ProductReferencesModule, OrderDetailsModule, PaymentMethodsModule, ConsentManagementModule, CartComponentModule, TabParagraphContainerModule, OrderConfirmationModule, ProductImagesModule, ProductSummaryModule, ProductVariantsModule, ProductIntroModule, BannerCarouselModule, MyCouponsModule, WishListModule, NotificationPreferenceModule, MyInterestsModule, StockNotificationModule, ReplenishmentOrderHistoryModule, ReplenishmentOrderConfirmationModule, ReplenishmentOrderDetailsModule, UserComponentModule, CloseAccountModule, UpdateEmailModule, UpdatePasswordModule, UpdateProfileModule, ForgotPasswordModule, ResetPasswordModule, -
Remove any use of the
CmsLibModulefrom your app.
Migrating the MainModule
- In the
SpartacusFeaturesModule, import theAnonymousConsentsDialogModulefrom@spartacus/storefront. - Remove any use of the
MainModulefrom you app.
Migrating the StorefrontFoundationModule
- In the
SpartacusFeaturesModule, import the following from@spartacus/core:AuthModule.forRoot()AnonymousConsentsModule.forRoot()CartModule.forRoot()CheckoutModule.forRoot()UserModule.forRoot()ProductModule.forRoot()
- In the
SpartacusFeaturesModule, import theCartPageEventModule,PageEventModuleandProductPageEventModulefrom@spartacus/storefront. - Remove any use of the
StorefrontFoundationModulefrom your app.
Migrating the OccModule
- In the
SpartacusFeaturesModule, import the following modules from@spartacus/core:AsmOccModuleCartOccModuleCheckoutOccModuleProductOccModuleUserOccModuleCostCenterOccModule
- Remove any use of the
OccModulefrom your app.
Migrating the EventsModule
- In the
SpartacusFeaturesModule, import theCartPageEventModule,PageEventModuleandProductPageEventModulefrom@spartacus/storefront. - Remove any use of the
EventsModulefrom your app.
Cleaning Up
Now that you have migrated your application to the new app structure, you have the opportunity to remove feature modules that were originally included in the core Spartacus libraries, but which you may not have been using. By removing unused feature modules, you can make your application’s initial load time faster.
The following is a list of common Spartacus features that you may not be using, and which you can remove from your application:
- If you do not use ASM, you can remove the
AsmModuleandAsmOccModuleimports from theSpartacusFeaturesModule. - If you do not use Smartedit, you can remove the
SmartEditModuleimport from theSpartacusFeaturesModule. - If you do not use Qualtrics, you can remove the
QualtricsModuleimport from theSpartacusFeaturesModule. - If you do not use product variants, you can remove the
ProductVariantsModuleimport from theSpartacusFeaturesModule - If you do not support order replenishment, you can remove the
ReplenishmentOrderHistoryModule,ReplenishmentOrderConfirmationModuleandReplenishmentOrderDetailsModuleimports from theSpartacusFeaturesModule.
There are many more modules that you may not need, and it is recommended to go through all of the imports in the SpartacusFeaturesModule and verify if you are using them or not. If not, simply remove the import to make your application smaller.
Upgrading Your Angular Libraries
Before upgrading Spartacus to 4.0, you first need to upgrade Angular to version 12, and upgrade Angular third party dependencies, such as @ng-bootstrap/ng-bootstrap and @ng-select/ng-select, to the version that is compatible with Angular 12.
The following is an example command that upgrades Angular to version 12, and also upgrades the related dependencies:
ng update @ng-bootstrap/ng-bootstrap@10 @ng-select/ng-select@7 @angular/core@12 @angular/cli@12
For more information, see the officialAngular Update Guide.
Upgrading Spartacus to 3.4.x
You must first upgrade all of your @spartacus libraries to the latest 3.4.x release before you begin upgrading to Spartacus 4.0. For more information, see Upgrading Spartacus Libraries to a New Minor Version.
Upgrading Spartacus to 4.0
Spartacus 4.0 includes many new features and fixes. Since this update is a major release, some of the updates may also be breaking changes for your application. In this case, additional work on your side may be required to fix issues that result from upgrading from 3.4.x to 4.0.
To update to version 4.0 of Spartacus, run the following command in the workspace of your Angular application:
ng update @spartacus/schematics@4
When the update has finished running, inspect your code for comments that begin with // TODO:Spartacus. For detailed information about each added comment, see the Detailed List of Changes.
Importing the App Routing Module from the Storefront Library
After upgrading to 4.0, you can delete your local app-routing.module file, if it exists. Instead of importing the local AppRoutingModule into your AppModule, you should import the AppRoutingModule from @spartacus/storefront.
Migrating from Accelerator to Spartacus
If you are considering migrating a project from Accelerator to Spartacus, see Migrate Your Accelerator-based Storefront to Project Spartacus on the SAP CX Works website.