π°οΈ AngularJS Service Workers & PWA β Turn AngularJS Apps into Offline-First Progressive Web Apps (2025 Guide)
π§² Introduction β Why Add Service Workers & PWA Support to AngularJS?
In the era of mobile-first and offline-first web apps, Progressive Web Apps (PWAs) are a must-have. Although AngularJS predates PWA support and service workers, you can still implement these features manually or with helper libraries to add:
- Offline capabilities
- Background sync
- Improved performance and caching
- PWA install prompts and app-like behavior
π― In this guide, youβll learn:
- What service workers and PWAs are
- How to register a service worker in AngularJS
- How to configure caching, offline support, and manifests
- Best practices for deploying a PWA-enabled AngularJS app
π What is a Service Worker?
A Service Worker is a background script that:
- Intercepts and handles network requests
- Caches files and API responses
- Works offline and syncs in the background
- Powers push notifications and background updates
π§± What is a PWA (Progressive Web App)?
A PWA combines the best of web and mobile apps. Features include:
- Responsive design
- Offline access
- Add-to-home-screen support
- Native-like performance
- HTTPS-only deployment
β AngularJS doesn’t support PWAs out-of-the-box but you can manually add these capabilities.
βοΈ Step 1: Create a service-worker.js
File
Place this file in your AngularJS project root (typically next to index.html
).
const CACHE_NAME = 'angularjs-pwa-cache-v1';
const urlsToCache = [
'/',
'/index.html',
'/styles.css',
'/app.js',
'/images/logo.png'
];
// Install SW and cache static assets
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
return cache.addAll(urlsToCache);
})
);
});
// Serve from cache
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
});
β This caches your static files and serves them offline.
βοΈ Step 2: Register the Service Worker in AngularJS
Add the following to your app.js
or directly inside a controllerβs run()
block:
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js')
.then(function(reg) {
console.log("Service Worker registered!", reg);
})
.catch(function(err) {
console.error("Service Worker registration failed:", err);
});
}
β Place this in the main JS entry point so it loads on every page.
π Step 3: Create a Web App Manifest
Create a file called manifest.json
in your root folder:
{
"name": "AngularJS PWA App",
"short_name": "NgJS PWA",
"start_url": "/index.html",
"display": "standalone",
"background_color": "#ffffff",
"theme_color": "#1976d2",
"icons": [
{
"src": "images/icon-192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "images/icon-512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}
β
Link it in index.html
:
<link rel="manifest" href="manifest.json">
π Step 4: Ensure HTTPS Deployment
Service workers and PWAs require HTTPS (except on localhost). Deploy to:
- GitHub Pages
- Netlify
- Firebase Hosting
- Any HTTPS-enabled server
π¦ Optional: Add Background Sync & Push Notifications
Advanced service workers support:
- Background data syncing
- Push notifications using
push
andsync
events - IndexedDB integration for offline data storage
You can explore libraries like:
- Workbox.js β For managing complex caching
- Sw-precache / sw-toolbox β Legacy tools usable in AngularJS projects
π§ Real-World Use Cases
Use Case | Feature |
---|---|
To-do apps | Cache tasks for offline use |
Product catalogs | Serve static product list |
News readers | Prefetch articles |
Booking forms | Sync requests when online |
Portfolio apps | Enable installable experience |
π οΈ Best Practices for AngularJS PWAs
βοΈ Keep service worker lean and simple
βοΈ Test caching and fallbacks thoroughly
βοΈ Use icons and manifest for installability
βοΈ Keep cache versioning in mind
βοΈ Serve app over HTTPS only
π Summary β Recap & Next Steps
Even without native support, AngularJS can be enhanced to function as a Progressive Web App using service workers and a manifest. This enables offline access, caching, and mobile-like installabilityβall while keeping your existing AngularJS stack.
π Key Takeaways:
- Add
service-worker.js
and register it in your AngularJS app - Use a manifest file to define PWA appearance
- Cache important static assets and pages
- Deploy over HTTPS to enable full PWA features
βοΈ Real-world Relevance:
Perfect for modernizing legacy AngularJS apps into mobile-ready, offline-friendly applications without full migration.
β FAQ β AngularJS Service Workers & PWA
β Can AngularJS work with service workers?
β
Yes. You can manually register and configure service workers in AngularJS projects.
β Is AngularJS compatible with PWAs?
β
AngularJS doesnβt natively support PWA features, but you can add service workers and manifest files manually.
β Do PWAs need HTTPS?
β
Yes. Service workers only run on secure origins (HTTPS) or localhost
.
β What libraries help with service workers in AngularJS?
β
You can use Workbox.js, sw-toolbox
, or sw-precache
.
Share Now :