Adding PWA Support to Spartacus
Note: Spartacus 3.x is no longer maintained. Please upgrade to the latest version.
Note: Spartacus 3.x was tested with SAP Commerce Cloud versions 1905 to 2105. Spartacus 3.x has not been verified to work with (and is not guaranteed to work with) SAP Commerce Cloud 2211 or later releases.
You can add Progressive Web Application (PWA) support to the Spartacus app. Spartacus supports the latest major version of @angular/pwa.
Setting up PWA in Spartacus
-
Add the Angular PWA dependency to your shell app, as follows:
ng add @spartacus/schematics --pwaNote: The command works even if you already have Spartacus installed.
Note: If you have more than one project in your workspace, use the
--projectflag to add PWA to your main project.If you already have the
@angular/pwadependency, do the following:- remove the
@angular/pwadependency frompackage.json - run
yarnto remove the dependencies from your app - run
ng add @spartacus/schematics --pwa
Adding the dependency using
ng add @spartacus/schematics --pwaalso triggers the angular PWA schematic, which automatically does the following:- creates default PWA configuration files
- creates the
ngsw-config.jsonservice worker configuration file - creates icons
- updates the
angular.jsonfile to include PWA-related resources in your build - updates project files for PWA readiness.
- remove the
-
Build your app in
prodmode, as follows:ng build --prodThe build generates the required files to register your service worker and serve your app in PWA mode.
Note: The Spartacus PWA module assumes a
production===trueflag, which is set using theenvironment.productionfile. You need to manually set this setting. -
Deploy and serve your app using an HTTP server.
For testing purposes, we recommend that you install http-server as a dev dependency, and that you serve the app using
http-server ./dist/your-appDouble-check that the service worker is running and that the Add to Home Screen feature works. For more information, see Adding the Spartacus App to the Home Screen.
Limitations
-
The default
ngserver (webpack devserver) does not provide support for service workers. You must use a separate HTTP server to support PWA. -
PWA is only supported in secure mode, so the HTTP server of your choice needs to serve Spartacus in secure (HTTPS) mode.