PWA — Service Workers & Offline Support
PWA = app jaisa website — install karo, offline bhi chalta hai
What is a PWA?
Progressive Web App — installable, works offline, fast loading. "PWA = app jaisa website — install bhi kar sakte ho, offline bhi chalta hai"
No app store needed — browser "Add to Home Screen". Examples: Twitter Lite, Starbucks, Pinterest.
Adding PWA to Angular
ng add @angular/pwaThis single command:
- Adds
@angular/service-workerpackage - Creates
ngsw-config.json(service worker config) - Registers service worker in
main.ts - Adds
manifest.webmanifest(app name, icons, theme color) - Updates
index.htmlwith manifest link and theme color meta tag
"Ek command — PWA setup ho gaya"
How Service Worker Works
- First visit: normal load, service worker installs in background
- Second visit: service worker intercepts network requests
- Caches static files (JS, CSS, images)
- Serves from cache when offline
"Service worker = cache manager — pehle download karo, phir offline bhi chalta hai"
User sees "Add to Home Screen" prompt automatically.
ngsw-config.json — Cache Configuration
{
"assetGroups": [{
"name": "app-shell",
"installMode": "prefetch",
"resources": {
"files": ["/favicon.ico", "/index.html"],
"versionedFiles": ["/*.js", "/*.css"]
}
}],
"dataGroups": [{
"name": "api-cache",
"strategy": "performance",
"maxSize": 50,
"timeout": "10s",
"patterns": ["/api/**"]
}]
}"assetGroups = static files, dataGroups = API data"
Cache strategy: freshness (always check network first) vs performance (cache first, fallback to network).
Offline Experience Best Practices
- Show "You're offline" banner using service worker events
- Cache critical API responses (dataGroups)
- Do NOT cache POST/PUT/DELETE requests
- Test: DevTools → Application → Service Workers → Offline checkbox
- "Offline bhi kaam karo — user ko pata bhi chalna chahiye ki offline hai"
Limitations: can't cache streaming, WebSockets, very large files.
Key Takeaways
- PWA = installable, offline-capable web app
- ng add @angular/pwa configures everything in one command
- Service worker intercepts requests and serves from cache
- ngsw-config.json controls caching strategy (assetGroups vs dataGroups)
- Test offline with DevTools Application tab
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login