Adding PWA Support to Spartacus
Note: Spartacus 1.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 @angular/pwaNote: If you have more than one project in your workspace, use the
--projectflag to add PWA to your main project.Note: If you already have the
@angular/pwadependency, do the following:- remove the
@angular/pwadependency frompackage.json - run
yarnto remove the dependencies from your app - reinstall the dependencies again using
ng.
Adding the dependency using
ngalso triggers the angular PWA schematic, which automatically does the following:- creates default PWA configuration files
- creates icons
- updates the
angular.jsonfile to include PWA related resources in your build - updates project files for PWA readiness.
- remove the
-
Update your
ngsw-config.json, as follows:{ "index": "/index.html", "assetGroups": [ { "name": "app", "installMode": "prefetch", "resources": { "files": ["/favicon.ico", "/index.html", "/*.css", "/*.js"] } } ] } -
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. This setting needs to be set manually by customers. -
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.