# Offline strategies

> Make a PWA load and stay usable with no network: precache the shell, route each fetch by request type, clean up old caches, and serve an offline fallback page.

A service worker can keep a PWA usable without a network connection by serving its shell
from cache, fetching data with a cached fallback, and returning a stored offline page when a
navigation misses both. Choose how to use the cache and network for each request; there is
no single "offline mode".

You need a registered service worker (follow [Getting started](/guides/getting-started/)
if you have none) and an understanding of when `install` and `activate` fire, described in
[Service worker lifecycle: install to update](/reference/service-worker/lifecycle/). Add each handler to the same `sw.js`; the Background Sync step also needs a `replayOutbox()` function that you write, to read the queue and resend it.

## Precache the shell on install

Open a versioned cache in `install` and `addAll()` the HTML, CSS, and JavaScript every view
needs plus the offline page. The version in the cache name is what lets the next release
replace the whole set. `addAll()` rejects if any single file fails to fetch, and a rejected
`waitUntil()` promise abandons the install, so list only files you control.

```js
const VERSION = 'v3';
const SHELL_CACHE = `shell-${VERSION}`;
const DATA_CACHE = `data-${VERSION}`;
const SHELL = ['/', '/app.css', '/app.js', '/offline.html'];

self.addEventListener('install', (event) => {
  event.waitUntil(caches.open(SHELL_CACHE).then((cache) => cache.addAll(SHELL)));
});
```

Shell files served with hashed names can stay cache-first for as long as the cache lives;
an unhashed `/app.js` only updates when the worker reinstalls, which a changed `sw.js` triggers; bumping `VERSION` also moves the files to a fresh cache.

## Route each fetch by request type

A navigation gets the network first so the user sees fresh HTML, with
the cached shell when the network fails. Shell assets are cache-first: instant, updated by
the next worker version. API responses are network-first with the last good copy as the
fallback, which costs one full network wait per request while online. Images and fonts use
stale-while-revalidate: the cached copy returns at once, and a background fetch refreshes
it, so a response can be stale until the refresh lands.

```js
self.addEventListener('fetch', (event) => {
  const { request } = event;
  if (request.method !== 'GET') return; // let POST and friends hit the network

  const url = new URL(request.url);
  if (request.mode === 'navigate') {
    event.respondWith(networkFirst(event, SHELL_CACHE, '/offline.html'));
  } else if (SHELL.includes(url.pathname)) {
    event.respondWith(caches.match(request).then((hit) => hit || fetch(request)));
  } else if (url.pathname.startsWith('/api/')) {
    event.respondWith(networkFirst(event, DATA_CACHE));
  } else if (request.destination === 'image' || request.destination === 'font') {
    event.respondWith(staleWhileRevalidate(event, DATA_CACHE));
  }
});

async function networkFirst(event, cacheName, fallbackPath) {
  const { request } = event;
  const cache = await caches.open(cacheName);
  try {
    const fresh = await fetch(request);
    if (fresh.ok) event.waitUntil(cache.put(request, fresh.clone()));
    return fresh;
  } catch {
    const cached = await cache.match(request);
    if (cached) return cached;
    if (fallbackPath) return cache.match(fallbackPath);
    throw new Error(`offline and ${request.url} is not cached`);
  }
}

async function staleWhileRevalidate(event, cacheName) {
  const { request } = event;
  const cache = await caches.open(cacheName);
  const cached = await cache.match(request);
  const refresh = fetch(request).then(async (response) => {
    if (response.ok) await cache.put(request, response.clone());
    return response;
  });
  event.waitUntil(refresh.catch(() => {})); // keep the worker alive until the cache write lands
  return cached || refresh;
}
```

`cache.put()` consumes the response body, so the code clones before storing and returns
the original. The strategies, their costs, and a Workbox mapping are compared in
[Choosing a caching strategy](/guides/offline/caching-strategies/).

## Delete old caches on activate

`activate` runs once the old worker no longer controls any page (or the new one called `skipWaiting()`), which makes
it the safe moment to delete the previous version's caches; pages opened before that stay uncontrolled until they reload. Caches are shared across the
whole origin, so delete only caches that match your own prefix. Keep this
handler short: fetches queue behind a long activation.

```js
self.addEventListener('activate', (event) => {
  event.waitUntil(
    caches.keys().then((names) =>
      Promise.all(
        names
          .filter((name) => (name.startsWith('shell-') || name.startsWith('data-')) && !name.endsWith(VERSION))
          .map((name) => caches.delete(name)),
      ),
    ),
  );
});
```

## Queue failed writes with Background Sync

`networkFirst` returns stale data for a `GET`; a failed `POST` needs a retry. Where
`ServiceWorkerRegistration.sync` exists, store the body in IndexedDB and register a sync
tag; the browser fires `sync` when connectivity returns. The API is Chromium-only (see
[Background Sync](/compatibility/background-sync/) for versions), so the page must also
work when the registration throws.

```js
// In the page, after the POST fails:
async function queueForSync(tag) {
  const registration = await navigator.serviceWorker.ready;
  if (!('sync' in registration)) return false; // show "saved locally, retry later" instead
  try {
    await registration.sync.register(tag);
    return true;
  } catch {
    return false;
  }
}

// In sw.js:
self.addEventListener('sync', (event) => {
  if (event.tag === 'outbox') event.waitUntil(replayOutbox(event.lastChance));
});
```

`event.lastChance` is `true` when the browser will make no further attempts after this one, so if the replay fails on that pass it is the moment
to tell the user the write was not sent.

## Check the storage budget before precaching large files

Every cache counts against the origin's quota, and eviction can remove all of it at once.
`navigator.storage.estimate()` returns a conservative `quota` and the current `usage` in
bytes; the numbers are approximate because of compression and deduplication. Request
`navigator.storage.persist()` for data that must survive, as described in
[Storage persistence, quotas, and eviction](/reference/storage/persistence/).

```js
const { usage, quota } = await navigator.storage.estimate();
console.log(`${(usage / 1048576).toFixed(1)} MB used of ${(quota / 1048576).toFixed(0)} MB`);
```

## Reload with the network switched off

Load the app once online so `install` runs, reload so the worker controls the page, open the views that call `/api/` so the data cache fills, then in Chrome DevTools open the **Network**
panel, select **Offline** from the **Network throttling** drop-down next to the **Disable
cache** checkbox, and reload. The shell paints from `shell-v3`, `/api/` requests return the
last cached copy, and a navigation to an uncached URL shows `/offline.html`. Test the
installed app from a cold start too; a warm tab may still be running on resources it loaded earlier.

:::note
Chrome's [Network panel reference](https://developer.chrome.com/docs/devtools/network/reference) says DevTools displays a warning icon next to the **Network** tab to remind you that offline is enabled. Chromium defines the network error `ERR_INTERNET_DISCONNECTED` ("The Internet connection has been lost") in [`net/base/net_error_list.h`](https://chromium.googlesource.com/chromium/src/+/main/net/base/net_error_list.h), and the Console reports failed requests in the form `Failed to load resource: net::ERR_...`, as in DevTools' own tests ([`test/conductor/events.ts`](https://github.com/ChromeDevTools/devtools-frontend/blob/main/test/conductor/events.ts)).
:::

## See also

- [Choosing a caching strategy](/guides/offline/caching-strategies/)
- [Service worker lifecycle: install to update](/reference/service-worker/lifecycle/)
- [Offline fallback for service workers](/reference/service-worker/offline-fallback/)
- [Storage persistence, quotas, and eviction](/reference/storage/persistence/)
- [The Offline Cookbook](https://web.dev/articles/offline-cookbook) (web.dev)
- [Background Synchronization API](https://developer.mozilla.org/en-US/docs/Web/API/Background_Synchronization_API) (developer.mozilla.org)
- [StorageManager: estimate() method](https://developer.mozilla.org/en-US/docs/Web/API/StorageManager/estimate) (developer.mozilla.org)

← Back to the [Guides](/guides/) overview.