Adding offline support to a web app: what is safe to store on the device

How to add offline support to a web app with a service worker: what to cache, what never to store on a shared phone, sync and conflict rules, and testing.

The short answer

Add offline support in layers: first cache the app files and show a proper offline page, then cache public pages, and only then store a signed-in user's data in per-user storage that is cleared at sign-out. Never cache signed-in pages or API responses by default, because devices are shared and caches are not reliably cleared. Decide what the service worker may keep before you write it.

On this page
  1. What does “offline” mean for your users?
  2. How does a service worker fit in?
  3. What is safe to store on the device?
  4. How do you store a user’s data offline when you must?
  5. How do you handle changes made offline?
  6. How do you test it?
  7. When is a native app the better answer?
  8. Working with us

“Can it work offline?” is a common request for field staff, venues with poor reception, and apps used on trains and in basements. In a web app it is possible, but it is two problems: making the app load without a network, and letting people read or change data without one. The second is much harder, and it is where privacy mistakes happen.

We faced this with Kommo, our hiring app, which installs from the browser. The details of that choice are in Kommo as an installable web app. This article generalizes the decisions.

What does “offline” mean for your users?

Write down what people need to do with no connection. The answers are very different:

  1. Open the app and see something sensible instead of the browser’s dinosaur. This is cheap.
  2. Read recent data such as today’s schedule or a task list. Medium effort, with privacy decisions.
  3. Create or change data and have it sync later. This is a distributed-systems problem: conflicts, retries and duplicates.

Most products need level 1, some need level 2, and few really need level 3. Do the cheapest level that solves the real problem.

How does a service worker fit in?

A service worker is a script that sits between your web app and the network. It can answer requests from a cache. It decides what to keep and for how long. That makes it powerful and dangerous: a cache entry you did not mean to keep stays on the device.

A conservative setup, which we use as a starting point:

  • Pre-cache very little: the manifest, icons and an offline page with what it needs. An earlier set-up of Kommo downloaded the whole build on the first visit, which cost every new visitor 3.3 MB.
  • Cache as people browse: scripts, styles, fonts and images.
  • Public pages: network first, last copy if offline.
  • Fallback: a clear offline page when nothing is available.

What is safe to store on the device?

Data Safe to cache? Notes
App code, styles, fonts, icons Yes Versioned, so old files do not stay forever
Public marketing pages and public job or product pages Yes It is public anyway
Map tiles Yes Not personal
Signed-in HTML pages No They contain someone’s data
API responses with personal or business data No, unless a deliberate per-user store See below
Authentication tokens, passwords Never in a cache Use secure, HTTP-only cookies
Messages and conversations Avoid Highest sensitivity

The reason is shared devices. A phone behind a salon counter, a tablet on a restaurant stand, a family laptop: people forget to sign out, and browser caches are not reliably cleared when they do. A copy of a person’s applications left on a shared device is a data leak.

How do you store a user’s data offline when you must?

If level 2 or 3 is a real need, do it deliberately:

  1. Store in IndexedDB, not in the HTTP cache, under a key that includes the user id.
  2. Store the minimum. The list the person needs today, not the whole database. No messages and no data about other people unless essential.
  3. Clear it at sign-out, when the session expires, and when a different user signs in.
  4. Encrypt if the risk justifies it. Browser encryption helps only if the key is not stored next to the data, so it is a partial measure. Consider also the lifetime of the data.
  5. Show the state. A visible “Offline. Last updated 14:05” so no one acts on stale data without knowing.
  6. Tell the user what is stored and how to clear it.

For our own app we wrote the options down and chose: cache only public pages and the app shell first; later, maybe store the professional’s own application list in per-user storage, cleared at sign-out and at session end, with no messages and no business data; never cache all pages.

How do you handle changes made offline?

This is level 3, and it needs rules:

  • Queue the change locally with a unique id, so replays do not create duplicates. The server must accept the id as an idempotency key.
  • Define conflicts. If two people changed the same record, who wins? Options: last write wins (simple, can lose data), reject and ask the user, or merge fields. Choose per record type.
  • Respect the state machine. A booking that was cancelled by someone else while you were offline cannot be seated. The server decides using the allowed moves, and the app shows the refusal: status columns as state machines.
  • Sync in the background with backoff, and make the queue visible so a user knows something is waiting.
  • Limit the scope. Allow offline edits to a few record types, not everything.

How do you test it?

Offline bugs only show up offline, so test with the network off:

  • The app opens with no network and shows the offline page where nothing is cached.
  • Public pages load from cache.
  • Signed-in pages and API calls are not in the cache after a session. Check the cache contents in a test.
  • Signing out empties the per-user store.
  • The install prompt, if you have one, follows the rules you wrote. Ours has browser tests for the manifest, the icons, service worker registration and the offline page.

When is a native app the better answer?

When people need a lot of data offline, background sync with the operating system, Bluetooth or other device features, or push notifications that work reliably on every platform. Start as a web app and move later; see a PWA or native apps for your first release.

Working with us

We build web applications, including installable web apps with a service worker that is limited on purpose. If you need offline work, tell us what people must be able to do without a network and what is sensitive in it, through the contact page, and see our web application work.

Keep reading

Tell us what you want to build.

A product, a website, a search problem or a system that should work better. We reply with a plain answer and the first step.

A few sentences are enough (at least 20 characters).

We use your details only to answer your inquiry. See our privacy policy.