A website can work perfectly well in a browser and still feel more like an app when people return to it. A Progressive Web App (PWA) uses standard web technologies to provide an installable experience and, when deliberately engineered, useful behavior during unreliable or missing network connections.
One distinction matters from the beginning: an install icon does not automatically make a website work offline. A web app manifest describes how the app should appear and launch. A service worker and a deliberate caching strategy provide custom offline behavior. This guide builds those pieces separately, with plain HTML, CSS, and JavaScript.
What is a Progressive Web App?
A PWA is a website enhanced with capabilities that supported browsers and operating systems can expose as an app-like experience. Depending on the platform, users may install it, launch it from an icon, and open it in a standalone window. It is still a web application: it is delivered through web technologies and remains subject to browser support and web-platform limits.
Think of the architecture as three layers:
- The web app: HTML, CSS, JavaScript, routes, and application data.
- The manifest: the app name, icons, launch URL, and display preferences.
- The service worker and caches: an optional layer that can intercept requests and return selected resources when the network is unavailable.
These layers solve different problems. A manifest does not cache files. A service worker does not create a polished app icon by itself. You need to design and test installation and offline behavior independently.
1. Create a web app manifest
A web app manifest is a JSON file linked from your HTML. It gives the browser information needed to present the site as an installable app. If your application has multiple pages, link the same manifest from every page that should participate in the app experience.
Create manifest.webmanifest in the app's public root:
{
"id": "./",
"name": "Field Notes",
"short_name": "Field Notes",
"description": "A simple offline-ready notes app",
"start_url": "./",
"scope": "./",
"display": "standalone",
"background_color": "#ffffff",
"theme_color": "#163a5f",
"icons": [
{
"src": "./icons/icon-192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "./icons/icon-512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}
Replace the sample name, description, colors, and icon paths with real assets. The files must exist at the URLs you declare; placeholder paths are not production assets.
Link the manifest from the document's <head>:
<link rel="manifest" href="./manifest.webmanifest">
Manifest fields worth understanding
nameandshort_name: the full and compact app names.start_url: the recommended page opened when the installed app launches. Keep it on the same origin as the page linking to the manifest.scope: the URL paths that belong to the app-like experience. Set it deliberately and use a trailing slash for directory scopes.display:standalonerequests an app-style window on platforms that support it.icons: provide real icons at the declared sizes. Chromium-based browsers document 192px and 512px icons among their installability requirements.theme_colorandbackground_color: influence browser and launch presentation where supported.
Installability differs by browser and operating system. MDN's current guide describes the manifest and HTTPS requirements for browser-promoted installation and explicitly notes that a service worker is not a universal installability requirement. The installation flow on iOS is different from Chromium's install prompt. Do not promise every visitor the same button or app-store-style experience; check the MDN PWA installability guide.
2. Use HTTPS and plan your URL paths
Service workers are available only in secure contexts. Production sites should use HTTPS; local development can use localhost or 127.0.0.1. Opening an HTML file directly from disk is not a substitute for testing through a local web server.
Pay attention to deployment paths. If your site lives at https://example.com/my-app/, a root-relative worker path such as /sw.js points to the domain root, not necessarily to your project. Relative paths such as ./sw.js are often safer for a project deployed in a subdirectory, as long as the manifest and worker are stored where the page expects them.
A service worker normally controls pages in the directory where its script lives and below. Put the worker at the level you intend to control. The MDN registration reference explains the default scope and secure-context restrictions.
3. Register the service worker
A service worker is a separate script that the browser can start when it needs to handle an event. Once active, it can intercept eligible requests from pages within its scope and return a response from the network or Cache API. It does not run continuously like a server process, so do not rely on its global variables surviving between events.
Register it from your page after load:
if ("serviceWorker" in navigator) {
window.addEventListener("load", () => {
navigator.serviceWorker
.register("./sw.js")
.then((registration) => {
console.log("Service worker scope:", registration.scope);
})
.catch((error) => {
console.error("Service worker registration failed:", error);
});
});
}
The script path is resolved relative to the page. Keep sw.js in the directory that should be controlled unless you have a specific reason to configure a narrower or broader scope.
4. Choose a caching strategy for each resource
Offline support is not the same as saving every response. Every cached response is a freshness decision: the app may show it again later even if the server has changed. Different resources need different strategies.
| Resource | Useful starting strategy | Why |
|---|---|---|
| Versioned CSS, JavaScript, icons | Cache first | Fast when filenames change with releases |
| HTML navigation | Network first with offline fallback | Prefer current pages while handling outages |
| Frequently changing API data | Network first or network only | Stale data may mislead users |
| Private account or payment data | Do not cache by default | A shared device or stale response can expose sensitive information |
These are starting points, not universal rules. A read-only reference app can tolerate more cached content than a booking or payment workflow. The MDN caching guide explains cache-first, network-first, and stale-while-revalidate strategies and the trade-off between responsiveness and freshness.
5. Build a small service worker
This example precaches a small app shell, uses the network for page navigation, and returns an offline page when navigation fails. It uses cache-first for same-origin static assets. It deliberately does not intercept POST requests or cache API responses.
Create sw.js next to the app's main HTML file. Change the asset list to files that really exist in your project:
const CACHE_PREFIX = "field-notes-";
const CACHE_NAME = "field-notes-v1";
const APP_SHELL = [
"./",
"./index.html",
"./styles.css",
"./app.js",
"./offline.html"
];
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL))
);
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(
keys
.filter((key) =>
key.startsWith(CACHE_PREFIX) && key !== CACHE_NAME
)
.map((key) => caches.delete(key))
)
).then(() => self.clients.claim())
);
});
self.addEventListener("fetch", (event) => {
const request = event.request;
if (request.method !== "GET") return;
const url = new URL(request.url);
if (url.origin !== self.location.origin) return;
if (request.mode === "navigate") {
event.respondWith(
fetch(request).catch(async () => {
const cache = await caches.open(CACHE_NAME);
return (await cache.match("./offline.html")) || Response.error();
})
);
return;
}
const isStaticAsset = ["style", "script", "image", "font"]
.includes(request.destination);
if (!isStaticAsset) return;
event.respondWith(
(async () => {
const cache = await caches.open(CACHE_NAME);
const cached = await cache.match(request);
if (cached) return cached;
const response = await fetch(request);
if (response.ok) {
await cache.put(request, response.clone());
}
return response;
})()
);
});
cache.addAll() fails if a listed resource cannot be fetched successfully. If you rename app.js or forget to deploy offline.html, the worker installation can fail. Start with a short list of essential files and verify every URL.
This example uses network-first for navigation because HTML often changes more frequently than versioned app-shell assets. Static assets are cached first for responsiveness. When the app shell changes, increment CACHE_NAME so the new worker can populate a new cache. The activation handler removes only old caches with this app's prefix; avoid deleting every cache on an origin that may host other applications.
6. Decide what “offline” means for your product
A cached interface can open while offline, but that does not mean every feature works. If a dashboard depends on a server request for its data, the interface may load while the dashboard remains empty. Write down which user tasks must work without a connection before you promise offline support.
- Static app shell: cache the HTML, CSS, JavaScript, icons, and offline page needed to start the interface.
- Read-only content: cache only content that can safely be shown later. Display a last-updated time when freshness matters.
- User-created records: use a structured local store such as IndexedDB when records must remain available offline.
- Server-dependent actions: show that an action is waiting to sync, and handle retries and duplicate submissions safely.
Offline writes require more design than a fetch handler. Consider conflicts, deleted records, authentication expiry, duplicate requests, and shared devices. A queued payment or booking must never appear completed merely because it was saved locally. Keep the server as the source of truth and show a pending state until the server confirms success.
Browser storage is not permanent backup. Users can clear site data, and browsers can evict stored data under storage pressure. Never keep the only copy of important business records in a browser cache.
7. Plan for service-worker updates
A newly downloaded service worker can wait while pages controlled by the previous worker remain open. This helps avoid replacing a worker in the middle of a session, but it also means users may not see a release immediately.
- Change the cache version when the app shell changes.
- Keep the install list aligned with files in the new release.
- Delete only the previous caches owned by this application.
- Test an existing installation updating to the new version, not just a clean first visit.
- If you add an update banner, explain what will happen before asking a user to reload.
Do not blindly call skipWaiting() and force a reload without considering unsaved work. An immediate update may be right for a small static app, but an editor or form may need to let the user finish first.
8. Test installability and offline behavior separately
Use a real HTTPS deployment or a local development server. Then test the two capabilities as separate checklists.
Installability checks
- Open the manifest in browser developer tools and confirm it loads without a JSON error.
- Check the name, start URL, scope, display mode, and icon files.
- Confirm HTTPS outside local development.
- Try the browser's own installation flow on the devices your audience uses.
- Launch the installed app and verify the expected page and display mode.
Offline checks
- Visit the site online once so the worker can install and precache its app shell.
- Wait until the worker is activated and controls the page.
- Simulate offline mode in developer tools, then reload and navigate.
- Confirm the offline page appears when a navigation cannot reach the network.
- Check that static assets load from cache while live-data features fail gracefully.
- Deploy a new version and test cache cleanup and the update experience.
Testing only the install prompt proves little about offline reliability. Testing only with a warm cache can hide a broken first-install path. Test a fresh browser profile, an existing installation, a slow connection, and a fully offline reload.
Common PWA mistakes
- Caching every request: this can store stale or private data and make debugging difficult.
- Using cache-first for changing HTML or API data: visitors may keep seeing outdated information.
- Using root-relative paths on a subdirectory deployment: the manifest, worker, icons, or start URL may point outside the project.
- Forgetting cache version changes: old CSS or JavaScript may remain in use after a release.
- Assuming offline means fully functional: network-dependent features still need explicit offline behavior.
- Assuming every browser installs the same way: support differs, so keep the normal website experience usable too.
Progressive Web Apps: common questions
Does a PWA always need a service worker?
No. A manifest is central to the installable web-app experience, but a service worker is not a universal installability requirement. It is commonly used when you want custom offline behavior and request caching.
Can a PWA work completely offline?
Only if you deliberately make the required interface and data available offline. A service worker can serve cached resources, but it cannot make an unavailable server, remote API, or online payment provider work without a connection.
Can I build a PWA with plain HTML, CSS, and JavaScript?
Yes. A manifest, service worker, and Cache API are browser features. You do not need React, Vue, or another frontend framework to create an installable, offline-aware web app.
Will a PWA install on every phone?
No. Installation support and the installation flow depend on the browser and operating system. Keep the website usable in a normal browser and test the exact devices your audience uses.
Is a PWA the same as a native app?
No. A PWA can provide an app-like launch and selected device integrations, but browser APIs and platform behavior differ. Verify deep operating-system, background, and hardware requirements before choosing a PWA architecture.
A practical way to start
Do not turn a simple website into a complex offline platform just to add an install button. Start with a correct manifest and a reliable install experience. Add a service worker when you have a clear reason to cache the app shell or support specific offline tasks. Then test the first visit, repeat visit, offline state, and update path on the devices your users actually have.
For related implementation topics, read our guide to reliable AI-assisted development, our Supabase vs Firebase backend comparison, and the website security checklist. If you need a responsive website or web application designed around real users, explore Raazim Tech's web development services.