PWA Basics
Turn websites into installable Progressive Web Apps with service workers and manifests.
What you'll learn
- What a Progressive Web App (PWA) is and its three pillars
- How the web app manifest enables installation and app-like behavior
- How service workers enable offline caching and background sync
- When a PWA is the right choice vs. a native app
Concept
PWA Basics
A Progressive Web App (PWA) is a web application that uses modern web APIs to deliver an app-like experience — installable on the home screen, working offline, sending push notifications, and running full-screen. PWAs are built with the same HTML, CSS, and JavaScript you already know, plus two key additions: a web app manifest and a service worker. They are "progressive" because they work in any browser, and enhance to app-like behavior where the browser supports it.
The Three Pillars
- Installable — the web app manifest lets the browser offer an "Add to Home Screen" prompt. Once installed, the PWA launches in its own window, without the browser chrome, with a custom icon and splash screen.
- Offline-capable — a service worker caches resources and API responses, so the app works without a network connection. This is the biggest difference from a traditional web app.
- App-like — full-screen display, custom themes, push notifications, and background sync make the PWA feel like a native app, not a website in a tab.
The Web App Manifest
The manifest is a JSON file linked from your HTML. It describes how the app should appear when installed:
{
"name": "My App",
"short_name": "MyApp",
"start_url": "/",
"display": "standalone",
"background_color": "#ffffff",
"theme_color": "#2563eb",
"icons": [{ "src": "icon-192.png", "sizes": "192x192" }]
}
The display: standalone setting makes it open without browser UI. The icons are used for the home screen and splash screen. Once a valid manifest and a registered service worker exist, the browser can prompt installation.
Service Workers
A service worker is a JavaScript file that runs in the background, separate from the page. It acts as a programmable network proxy — it intercepts every fetch request and can respond from a cache instead of the network. This is how offline support works:
- On install, the service worker pre-caches the app shell (HTML, CSS, JS).
- On fetch, it checks the cache first (fast, offline-capable), then falls back to the network.
- On network success, it updates the cache for next time.
This "cache-first, network-fallback" strategy means the app loads instantly from cache and works offline. Other strategies (network-first, stale-while-revalidate) suit different needs.
When to Use a PWA
PWAs shine for content apps (news, catalogs), tools (editors, calculators), and any app where cross-platform reach matters more than deep native APIs. They are not ideal for apps needing heavy device access (camera, Bluetooth, files) or high-performance graphics (games). The biggest advantage: one codebase runs everywhere, with no app store review process. The biggest limitation: browser support for advanced APIs varies, and iOS Safari has historically lagged behind Chrome on PWA features.
Example
// Register the service worker (in your main JS)
if ("serviceWorker" in navigator) {
window.addEventListener("load", () => {
navigator.serviceWorker.register("/sw.js")
.then(reg => console.log("SW registered:", reg.scope))
.catch(err => console.error("SW failed:", err));
});
}
// The service worker itself (sw.js)
const CACHE = "my-app-v1";
const ASSETS = ["/", "/index.html", "/styles.css", "/app.js"];
// Install: pre-cache the app shell
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE).then(cache => cache.addAll(ASSETS))
);
});
// Fetch: cache-first, then network
self.addEventListener("fetch", (event) => {
event.respondWith(
caches.match(event.request).then(cached => {
return cached || fetch(event.request).then(response => {
// Cache new responses for next time
const clone = response.clone();
caches.open(CACHE).then(cache => cache.put(event.request, clone));
return response;
});
})
);
});
The main script registers the service worker after page load. The service worker pre-caches the app shell on install, then serves requests from cache first, falling back to network. New network responses are cached for next time. This makes the app load instantly and work offline.
Try it
- JSON Formatter
The web app manifest is JSON — format and validate it before deploying.
- JSON Validator
Validate your manifest.json syntax — a malformed manifest breaks installation.
Common mistakes
The mistake
Forgetting that service workers require HTTPS (except localhost for development).
The fix
Service workers only register over HTTPS in production. Use HTTPS for your deployed site (Let's Encrypt provides free certificates). localhost is exempt for development. Without HTTPS, the service worker will not register and the PWA will not work.
The mistake
Caching too aggressively and never updating cached content.
The fix
Version your cache name (e.g., my-app-v1) and update it when content changes. Use stale-while-revalidate for data that updates frequently. Listen for the 'activate' event to clean up old caches. Otherwise, users see stale content forever.
Related Resources
Related Cheatsheets
- JavaScript Cheatsheet
Reference for service worker lifecycle events and the Cache API.
Practice PWA Basics
2 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.