Day 22 โ Progressive Web Apps (PWA): Offline Support & Installation
๐ Day 22 โ Progressive Web Apps (PWA): Offline Support & Installation
Zero to Hero โ Hands-on Angular Tutorial
Today you will learn:
- โ๏ธ What is a PWA?
- โ๏ธ Adding PWA support (
@angular/pwa) - โ๏ธ The Manifest File (Identity, Icons, Theme Color)
- โ๏ธ The Service Worker (Offline Caching)
- โ๏ธ Cache Strategies (Performance vs Freshness)
- โ๏ธ Handling App Updates (Auto-Update logic)
A PWA allows your website to be installed on a phone/desktop and work without an internet connection. ๐ฑ
๐ฆ 1. Turn your App into a PWA
Angular makes this trivial. Run one command:
ng add @angular/pwa
This automates several tasks:
- Creates
manifest.webmanifest(App config). - Creates
ngsw-config.json(Caching rules). - Adds icons to
src/assets/icons. - Enables Service Worker in
app.config.ts. - Updates
index.html(Theme color, meta tags).
Note: Service Workers only work in Production mode (or http-server). They do not typically work with ng serve.
๐ฉ 2. The Manifest File
This file tells the browser โI am an app.โ
src/manifest.webmanifest:
{
"name": "My Angular App",
"short_name": "AngularApp",
"theme_color": "#1976d2",
"background_color": "#fafafa",
"display": "standalone", // ๐ Hides browser address bar!
"scope": "/",
"start_url": "/",
"icons": [ ... ]
}
display: "standalone": Makes it look like a native app.icons: The icon on the userโs home screen.
๐ง 3. Offline Caching (ngsw-config.json)
By default, Angular caches your HTML, JS, CSS, and Assets. If your server dies, the app still loads! ๐คฏ
But what about Data (API calls)? You must configure it manually.
ngsw-config.json:
{
"index": "/index.html",
"assetGroups": [ ... ], // Caches JS/CSS
"dataGroups": [
{
"name": "api-freshness",
"urls": [
"/api/daily-news",
"/api/stock-prices"
],
"cacheConfig": {
"strategy": "freshness", // ๐ Try Network first. Fallback to Cache.
"maxSize": 100,
"maxAge": "1h",
"timeout": "5s" // If network slow > 5s, use cache.
}
},
{
"name": "api-performance",
"urls": [
"/api/products", // Product list rarely changes
"https://fonts.googleapis.com/**"
],
"cacheConfig": {
"strategy": "performance", // ๐ Use Cache FIRST. Update in background.
"maxSize": 100,
"maxAge": "3d"
}
}
]
}
- Freshness: For data that changes often (News, Stock Prices).
- Performance: For static data (Fonts, Images, Product Catalog).
๐ฅ 4. Notify User of Updates
When you deploy a new version, users might still see the old cached version. Angularโs SwUpdate service handles this.
app.component.ts:
import { Component, OnInit } from '@angular/core';
import { SwUpdate } from '@angular/service-worker';
@Component({...})
export class AppComponent implements OnInit {
constructor(private swUpdate: SwUpdate) {}
ngOnInit() {
if (this.swUpdate.isEnabled) {
// Check for updates
this.swUpdate.versionUpdates.subscribe(evt => {
if (evt.type === 'VERSION_READY') {
// ๐ Update found!
if (confirm('New version available. Load New Version?')) {
window.location.reload();
}
}
});
}
}
}
๐ซ 5. Testing PWA Locally
Since ng serve doesnโt support SW fully:
- Build:
ng build - Serve:
http-server -p 8080 -c-1 dist/my-app/browser - Open Chrome DevTools -> Application Tab.
- Look for Service Workers. Check โOfflineโ box and refresh the page.
- If it loads, YOU DID IT! ๐
๐ End of Day 22 โ What You Learned
Today you made your app usable Offline and Installable:
- โ๏ธ Manifest: Configured app Identity.
- โ๏ธ Service Worker: Intercepted network requests.
- โ๏ธ Caching Strategies: Tuned for speed (Performance) vs accuracy (Freshness).
- โ๏ธ SwUpdate: Prompting users to update.
๐งช Day 22 Challenge
โOffline Wikipediaโ (Mock)
- Add PWA support.
- Create a
dataGroupinngsw-config.jsonforhttps://api.github.com/users/*. - Use strategy: Performance.
- Build and serve (http-server).
- Load your Github Profile in the app.
- Turn off WiFi.
- Refresh the page. Your profile should still load instantly from cache!