The PWA Engineer makes web apps installable, offline-capable, and fast on slow connections. A PWA is not a separate codebase - it is the same web app with a service worker and a manifest. The service worker is the key: it intercepts all network requests and can serve cached responses even when the network is down.
Web App Manifest
{
"name": "My App",
"short_name": "App",
"description": "Description for the install prompt.",
"start_url": "/?source=pwa",
"display": "standalone",
"orientation": "portrait",
"background_color": "#ffffff",
"theme_color": "#3b82f6",
"icons": [
{ "src": "/icons/icon-192.png", "sizes": "192x192", "type": "image/png" },
{ "src": "/icons/icon-512.png", "sizes": "512x512", "type": "image/png" },
{ "src": "/icons/icon-512-maskable.png", "sizes": "512x512", "type": "image/png", "purpose": "maskable" }
],
"screenshots": [
{ "src": "/screenshots/mobile.png", "sizes": "390x844", "type": "image/png", "form_factor": "narrow" }
]
}
Service Worker Caching Strategies
// workbox-based service worker (use via next-pwa, vite-plugin-pwa, or directly)
import { registerRoute } from 'workbox-routing';
import { CacheFirst, NetworkFirst, StaleWhileRevalidate } from 'workbox-strategies';
import { ExpirationPlugin } from 'workbox-expiration';
import { precacheAndRoute } from 'workbox-precaching';
// Precache: assets that are part of the app shell (JS, CSS, HTML)
precacheAndRoute(self.__WB_MANIFEST);
// Cache First: images and fonts - rarely change, freshness not critical
registerRoute(
({ request }) => request.destination === 'image' || request.destination === 'font',
new CacheFirst({
cacheName: 'media-cache',
plugins: [new ExpirationPlugin({ maxEntries: 100, maxAgeSeconds: 30 * 24 * 60 * 60 })],
})
);
// Network First: API calls - fresh data preferred, cached fallback if offline
registerRoute(
({ url }) => url.pathname.startsWith('/api/'),
new NetworkFirst({
cacheName: 'api-cache',
plugins: [new ExpirationPlugin({ maxEntries: 50, maxAgeSeconds: 5 * 60 })],
networkTimeoutSeconds: 3, // Fall back to cache if network takes >3s
})
);
// Stale-While-Revalidate: HTML pages - serve cache instantly, update in background
registerRoute(
({ request }) => request.mode === 'navigate',
new StaleWhileRevalidate({ cacheName: 'pages-cache' })
);
Install Prompt Handling
// Capture the beforeinstallprompt event before it fires
let deferredPrompt: BeforeInstallPromptEvent | null = null;
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault(); // Stop the default mini-infobar
deferredPrompt = e as BeforeInstallPromptEvent;
showInstallButton(); // Show your custom install UI
});
async function handleInstallClick() {
if (!deferredPrompt) return;
deferredPrompt.prompt();
const { outcome } = await deferredPrompt.userChoice;
// outcome: 'accepted' | 'dismissed'
deferredPrompt = null;
hideInstallButton();
}
// Detect if already installed
window.addEventListener('appinstalled', () => {
deferredPrompt = null;
hideInstallButton();
analytics.track('pwa_installed');
});