Angular Progressive Web Apps (PWA)
Progressive Web Apps (PWAs) provide a native app-like experience using web technologies. This guide covers setting up and using Angular to create a PWA.
Setting Up Angular PWA
First, ensure that your Angular application is set up to use PWA by adding the necessary dependencies:
ng add @angular/pwa
Configuring Angular PWA
After adding PWA support, update your application to configure the service worker and manifest:
// ngsw-config.json
{
"index": "/index.html",
"assetGroups": [
{
"name": "app",
"installMode": "prefetch",
"resources": {
"files": [
"/favicon.ico",
"/index.html",
"/*.css",
"/*.js"
]
}
},
{
"name": "assets",
"installMode": "lazy",
"updateMode": "prefetch",
"resources": {
"files": [
"/assets/**",
"/*.png",
"/*.svg",
"/*.jpg",
"/*.woff2"
]
}
}
]
}
Manifest Configuration
Update the manifest.webmanifest
file to configure your app's manifest:
// manifest.webmanifest
{
"name": "Angular PWA",
"short_name": "PWA",
"theme_color": "#1976d2",
"background_color": "#fafafa",
"display": "standalone",
"scope": "/",
"start_url": "/",
"icons": [
{
"src": "assets/icons/icon-72x72.png",
"sizes": "72x72",
"type": "image/png"
},
{
"src": "assets/icons/icon-96x96.png",
"sizes": "96x96",
"type": "image/png"
},
{
"src": "assets/icons/icon-128x128.png",
"sizes": "128x128",
"type": "image/png"
},
{
"src": "assets/icons/icon-144x144.png",
"sizes": "144x144",
"type": "image/png"
},
{
"src": "assets/icons/icon-152x152.png",
"sizes": "152x152",
"type": "image/png"
},
{
"src": "assets/icons/icon-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "assets/icons/icon-384x384.png",
"sizes": "384x384",
"type": "image/png"
},
{
"src": "assets/icons/icon-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}
Service Worker Registration
Ensure the service worker is registered in your application:
// main.ts
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
if ('serviceWorker' in navigator && environment.production) {
navigator.serviceWorker.register('/ngsw-worker.js');
}
Handling Service Worker Events
Optionally, handle service worker events in your application:
// app.component.ts
import { Component, OnInit } from '@angular/core';
import { SwUpdate } from '@angular/service-worker';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
constructor(private swUpdate: SwUpdate) {}
ngOnInit() {
if (this.swUpdate.isEnabled) {
this.swUpdate.available.subscribe(() => {
if (confirm('New version available. Load New Version?')) {
window.location.reload();
}
});
}
}
}
Building and Testing PWA
Build and test your Angular PWA using the following commands:
ng build --prod
http-server -p 8080 -c-1 dist/app
Key Points
- Progressive Web Apps (PWAs) provide a native app-like experience using web technologies.
- Use the
@angular/pwa
package to add PWA support to your application. - Configure the service worker and manifest in the
ngsw-config.json
andmanifest.webmanifest
files. - Ensure the service worker is registered in your application.
- Optionally handle service worker events in your application to manage updates.
- Build and test your Angular PWA to ensure it works as expected.
Conclusion
Progressive Web Apps (PWAs) enhance the user experience by providing a native app-like experience using web technologies. By setting up Angular PWA, configuring the service worker and manifest, and handling service worker events, you can create a powerful and responsive PWA. Happy coding!