When a page is not in the cache and the network is gone, the browser shows its own error. A PWA should show its own page instead: same look, a clear message, and something useful to do.
1. Create the page
Keep it small and self-contained: inline the CSS, or make sure everything it uses is precached.
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>You are offline</title>
<style>body{font:16px/1.5 system-ui;margin:0;display:grid;place-items:center;min-height:100vh;padding:2rem}</style></head>
<body>
<main>
<h1>You're offline</h1>
<p>This page isn't saved on your device yet. Check your connection, or read one of these:</p>
<ul id="saved"></ul>
<button onclick="location.reload()">Try again</button>
</main>
</body>
</html>
2. Precache it, and serve it for failed navigations
const OFFLINE = '/offline.html';
self.addEventListener('install', (e) => e.waitUntil(caches.open('shell-v1').then((c) => c.add(OFFLINE))));
self.addEventListener('fetch', (event) => {
if (event.request.mode !== 'navigate') return;
event.respondWith(
fetch(event.request).catch(async () => (await caches.match(event.request)) || caches.match(OFFLINE))
);
});
Only navigations get the offline page. For images, you can return a small placeholder SVG instead.
3. List what is available offline
The page itself can read the caches and link to pages the visitor already has:
const list = document.getElementById('saved');
for (const name of await caches.keys()) {
const cache = await caches.open(name);
for (const req of await cache.keys()) {
const url = new URL(req.url);
if (url.origin === location.origin && url.pathname.endsWith('/')) {
const li = document.createElement('li');
li.innerHTML = `<a href="${url.pathname}">${url.pathname}</a>`;
list.append(li);
}
}
}
PWA.live's own offline page does this, reading each page's title from the cache.
4. Retry automatically
addEventListener('online', () => location.reload());
5. Let people save pages on purpose
A "Save for offline" button that caches a page (and its images) on request is often more useful than guessing. Every guide on PWA.live has one: it sends the URL to the service worker, which stores it in a separate cache that is never trimmed automatically.
Translations
If your site has several languages, make one offline page per language and pick it by the URL's language prefix in the service worker. Our offline page generator builds a ready-to-use page in any language.