A Progressive Web App (PWA) delivers an app-like experience through the browser. Angular provides first-class support for building PWAs using service workers and manifest files.
PWAs are web applications that use modern browser capabilities to provide features traditionally available only to native apps: offline support, push notifications, install prompts, and fast loading. Key characteristics include:
The @angular/service-worker package integrates a service worker into your Angular application. Add it using the Angular CLI.
ng add @angular/pwa
This command sets up the service worker, creates a manifest.webmanifest file, and configures ngsw-config.json.
Register the service worker in your application module:
// app.module.ts
import { ServiceWorkerModule } from '@angular/service-worker';
import { environment } from '../environments/environment';
@NgModule({
imports: [
ServiceWorkerModule.register('ngsw-worker.js', {
enabled: environment.production,
registrationStrategy: 'registerWhenStable:30000'
})
]
})
export class AppModule { }
This configuration file defines how the service worker caches resources and handles data requests.
Asset groups define which static resources (HTML, CSS, JS, images) to cache and how.
{
"assetGroups": [
{
"name": "app",
"installMode": "prefetch",
"updateMode": "lazy",
"resources": {
"files": [
"/favicon.ico",
"/index.html",
"/*.css",
"/*.js"
]
}
},
{
"name": "assets",
"installMode": "lazy",
"updateMode": "lazy",
"resources": {
"files": [
"/assets/**"
]
}
}
]
}
Data groups define caching strategies for API calls and other dynamic data.
{
"dataGroups": [
{
"name": "api",
"urls": ["/api/**"],
"cacheConfig": {
"maxSize": 100,
"maxAge": "1d",
"timeout": "5s",
"strategy": "performance"
}
}
]
}
strategy property can be performance (cache-first) or freshness (network-first).Angular's service worker supports several caching strategies:
Trigger the browser's native install prompt to allow users to add your app to their home screen.
// install-prompt.service.ts
import { Injectable } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class InstallPromptService {
private deferredPrompt: any;
isInstallable = false;
constructor() {
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
this.deferredPrompt = e;
this.isInstallable = true;
});
}
install() {
this.deferredPrompt?.prompt();
this.deferredPrompt = null;
this.isInstallable = false;
}
}
Once the service worker is active and resources are cached, your application can function without a network connection. Monitor online/offline status with Angular's @HostListener.
@HostListener('window:offline')
onOffline() { this.isOnline = false; }
@HostListener('window:online')
onOnline() { this.isOnline = true; }
The manifest.webmanifest file controls how your app appears when installed. It includes the app name, icons, theme color, and display mode.
{
"name": "Angular PWA",
"short_name": "AngularPWA",
"theme_color": "#1976d2",
"background_color": "#fafafa",
"display": "standalone",
"scope": "/",
"start_url": "/",
"icons": [
{ "src": "assets/icons/icon-192x192.png", "sizes": "192x192", "type": "image/png" },
{ "src": "assets/icons/icon-512x512.png", "sizes": "512x512", "type": "image/png" }
]
}
Use Google Lighthouse (built into Chrome DevTools) to audit your PWA. It checks for installability, offline support, performance, and best practices. Aim for a score of 90+ on all categories.
@angular/pwa package to an existing Angular project. Configure ngsw-config.json to cache all API calls under /api/** with a performance strategy. Verify offline functionality using Chrome DevTools.