PWA workflow
Initialize the Service Worker and progressive web app capabilities.
Add the service worker package
In your app's package.json add the latest Angular service worker package:
"@angular/service-worker": "^15.2.0",
Run npm i
to install the package.
Update the application Index
path: apps\{{project}}\src\index.html
Add
the following directly before the head
closing tag
<link rel="manifest" href="manifest.webmanifest">
<meta name="theme-color" content="#1976d2">
Add the web manifest file
Create a manifest.webmanifest
file at the following location:
apps\{{project}}\src\manifest.webmanifest
Add the following code:
{
"name": "{{project}}",
"short_name": "{{project}}",
"theme_color": "#1976d2",
"background_color": "#fafafa",
"display": "standalone",
"scope": "./",
"start_url": "./",
"icons": [
{
"src": "assets/icons/icon-72x72.png",
"sizes": "72x72",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-96x96.png",
"sizes": "96x96",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-128x128.png",
"sizes": "128x128",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-144x144.png",
"sizes": "144x144",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-152x152.png",
"sizes": "152x152",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-192x192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-384x384.png",
"sizes": "384x384",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-512x512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable any"
}
]
}
Add the web ngsw-config file
Create a ngsw-config.json
file at the following location:
apps\{{project}}\src\ngsw-config.json
Add the following code:
{
"$schema": "../../../node_modules/@angular/service-worker/config/schema.json",
"index": "/index.html",
"assetGroups": [
{
"name": "app",
"installMode": "prefetch",
"resources": {
"files": [
"/favicon.ico",
"/index.html",
"/manifest.webmanifest",
"/*.css",
"/*.js"
]
}
},
{
"name": "assets",
"installMode": "lazy",
"updateMode": "prefetch",
"resources": {
"files": [
"/assets/**",
"/*.(svg|cur|jpg|jpeg|png|apng|webp|avif|gif|otf|ttf|woff|woff2)"
]
}
}
]
}
Update the application project.json
Add the
manifest.webmanifest
to the project assetsAdd the
"serviceWorker": true,
to the project optionsAdd the
"ngswConfigPath": "apps/{{project}}/src/ngsw-config.json"
to the project options
...
"assets": [
...
"apps/{{project}}/src/manifest.webmanifest",
...
],
...
"serviceWorker": true,
"ngswConfigPath": "apps/full-stack-typescript/src/ngsw-config.json"
},
Update the application module
Update the app.module
file to include the following:
...
import { isDevMode, ... } from '@angular/core';
import { ServiceWorkerModule } from '@angular/service-worker';
@NgModule({
...
imports: [
...
ServiceWorkerModule.register('ngsw-worker.js', {
enabled: !isDevMode(),
// Register the ServiceWorker as soon as the application is stable
// or after 30 seconds (whichever comes first).
registrationStrategy: 'registerWhenStable:30000'
}),
...
Add required icon assets
Add the required icon assets to the following location:
apps\{{project}}\src\assets\icons
Required icons:
icon-72x72.png
icon-96x96.png
icon-128x128.png
icon-144x144.png
icon-152x152.png
icon-192x192.png
icon-384x384.png
icon-512x512.png
Angular service worker documenation
Officical Angular documentation: https://angular.io/guide/service-worker-intro
The steps in this guide are provided for easy service worker implementation in a NX monorepo architecture. The Angular guide does not document the required considerations and as a result cannot be added automatically with the ng add
command.
Last updated