Adding PWA Support to Spartacus
Note: Spartacus 2.x is no longer maintained. Please upgrade to the latest version.
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 --pwa
Note: The command works even if you already have Spartacus installed.
Note: If you have more than one project in your workspace, use the
--project
flag to add PWA to your main project.If you already have the
@angular/pwa
dependency, do the following:- remove the
@angular/pwa
dependency frompackage.json
- run
yarn
to remove the dependencies from your app - run
ng add @spartacus/schematics --pwa
Adding the dependency using
ng add @spartacus/schematics --pwa
also triggers the angular PWA schematic, which automatically does the following:- creates default PWA configuration files
- creates the
ngsw-config.json
service worker configuration file - creates icons
- updates the
angular.json
file to include PWA-related resources in your build - updates project files for PWA readiness.
- remove the
-
Build your app in
prod
mode, as follows:ng build --prod
The 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===true
flag, which is set using theenvironment.production
file. 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-app
Double-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
ng
server (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.