François Facon
Ridae Hamdani
How to create an Angular Progressive Web App?
Progressive web apps use modern web APIs along with traditional progressive enhancement strategy to create cross-platform web applications.
Dev builds
Web App Manifest
Service Worker
API
Web App Manifest
Service worker is the backbone of PWA
$ ng new myPWA$ ng add @angular/pwa --project=myPWAText
Text
Text
this configuration file specifies which files and data URLs the Angular service worker should cache and how it should update the cached files and data.
"serviceWorker": true,
"ngswConfigPath": "src/ngsw-config.json" "configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"buildOptimizer": true,"src/manifest.json""build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/ng2ecom",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets",
],
...
},$ npm install @angular/service-worker --save
import { ServiceWorkerModule } from '@angular/service-worker';import { environment } from '../environments/environment';...
@NgModule({
...
imports: [
...
]
})
export class AppModule { } ServiceWorkerModule.register('/ngsw-worker.js',
{ enabled: environment.production }),$ ng build --prodnode_modules/@angular...
$ npm install -g http-server
Install http-server globally.
$ http-server -c-1 -P http://localhost:8080 .To launch the PWA from the dist folder just type
Thanks to the -P option all request to your API will be proxied to http://localhost:8080
"scripts": {
"ng": "ng",
"start": "ng serve --proxy-config ./proxy.json --live-reload false",
...
...
},Or add the following script in your package.json
"start:prod": "ng build --prod && cd dist/ng2ecom && http-server -c-1 -P http://localhost:8080 .",$ npm run start:prodTo launch the PWA with a simple
NGSW Debug Info:
Driver state: NORMAL ((nominal))
Latest manifest hash: cd4716ff2d3e24f4292010c929ff429d9eeead73
Last update check: 9s215u
=== Version 34c3fd2361735b1330a23c32880640febd059305 ===
Clients: 7eb10c76-d9ed-493a-be12-93f305394a77
=== Version cd4716ff2d3e24f4292010c929ff429d9eeead73 ===
Clients: ee22d69e-37f1-439d-acd3-4f1f366ec8e1
=== Idle Task Queue ===
Last update tick: 4s602u
Last update run: 9s222u
Task queue:
Debug log:import { SwUpdate } from '@angular/service-worker';
constructor(private swUpdate: SwUpdate) {}ngOnInit() {
if (this.swUpdate.isEnabled) {
this.swUpdate.available.subscribe(() => {
if (confirm(`New version available. Load New Version?`))
{ window.location.reload(); }
});
}
}ng build --prod --serviceWorker
...deployrm dist/ngsw.json
...deploy{
"index": "/index.html",
"assetGroups": [...],
"dataGroups": [...]
}index: Specifies the file that serves as the index page to satisfy navigation requests.
assetGroups: Assets are resources that are part of the app version that update along with the app. They can include resources loaded from the page's origin as well as third-party resources loaded from CDNs and other external URLs.
{
"name": "app",
"installMode": "prefetch",
"resources": {...}
}The installMode determines how these resources are initially cached.
prefetch tells the Angular service worker to fetch every single listed resource while it's caching the current version of the app. This is bandwidth-intensive but ensures resources are available whenever they're requested, even if the browser is currently offline.
"resources": {
} "files": [
"/favicon.ico",
"/index.html",
"/*.css",
"/*.js"
]{
"name": "assets",
"installMode": "lazy",
"updateMode": "prefetch",
"resources": {...}
}The installMode determines how these resources are initially cached.
lazy does not cache any of the resources up front. Instead, the Angular service worker only caches resources for which it receives requests. This is an on-demand caching mode. Resources that are never requested will not be cached.
{
"name": "assets",
"installMode": "lazy",
"updateMode": "prefetch",
"resources": {...}
}For resources already in the cache, the updateMode determines the caching behavior when a new version of the app is discovered.
prefetch tells the service worker to download and cache the changed resources immediately.
"resources": {
} "files": [
"/assets/**"
], "urls": [
"https://fonts.googleapis.com/**",
"https://fonts.gstatic.com/**"
]{
"name": "api-performance-data",
"urls": ["data/**"],
}Data requests are cached according to manually-configured policies
{
"name": "api-performance-data",
"urls": ["data/**"],
}"cacheConfig" : { "maxSize":1000, "maxAge":"3d",
"timeout":"10s", "strategy":"performance"}Cache Config : This section defines the policy by which matching requests will be cached.
{
"name" : "api-freshness-news",
"urls" : [ "api/**" ],
}"cacheConfig" : { "maxSize":1000, "maxAge":"1d",
"timeout":"10s", "strategy":"freshness"}The Angular service worker can use either of two caching strategies for data resources.
LightHouse
=>
JavaScript Libraries for adding offline support to web apps
workbox
A set of modules for different programming languages that make easy to send push messages
Web-push libs
Web Notification
Push API
2
François Facon
Ridae Hamdani