How To Handle Data Storage Problem with HTML5

Editor's note (2026): We've updated this 2014 post. AppCache, which the original recommended, has been removed from all major browsers; service workers replaced it. We've also fixed the code (curly quotes and missing spaces broke the original) and corrected the claim that localStorage stores data "securely."

Websites used to depend completely on a live connection. HTML5 and modern browser APIs let web apps store data in the browser, load faster, and keep working offline. Here are the main options.

localStorage

Stores simple key/value data (strings) with no expiration date. Data stays until it's cleared, and any page on the same origin (protocol + domain + port) can read it.

localStorage.setItem('theme', 'dark');
const theme = localStorage.getItem('theme');

Note: localStorage isn't secure. Any script running on your page can read it, so never store passwords, tokens, or personal data there.

sessionStorage

Works like localStorage, but data lasts only until the browser tab is closed, and it isn't shared between tabs.

sessionStorage.setItem('step', '2');

IndexedDB

A full database in the browser for large amounts of structured data, including files. It's ideal for offline-capable apps.

Open a database and create a store:

let db;

const request = indexedDB.open('album', 1);

request.onerror = (e) => console.error(e);

// Runs when the database is created or its version changes
request.onupgradeneeded = (e) => {
  db = e.target.result;
  const store = db.createObjectStore('cds', { keyPath: 'id', autoIncrement: true });
  store.createIndex('title', 'title', { unique: true });
  store.createIndex('band', 'band', { unique: false });
};

request.onsuccess = (e) => {
  db = e.target.result;
  listAlbums();
};

Add and remove items:

function addAlbum(album) {
  const tx = db.transaction(['cds'], 'readwrite');
  tx.objectStore('cds').add(album);
}

function removeAlbum(id) {
  const tx = db.transaction(['cds'], 'readwrite');
  tx.objectStore('cds').delete(id);
}

List everything with a cursor:

function listAlbums() {
  const store = db.transaction(['cds']).objectStore('cds');
  store.openCursor().onsuccess = (e) => {
    const cursor = e.target.result;
    if (cursor) {
      const { title, band, genre, year } = cursor.value;
      console.log(title, band, genre, year);
      cursor.continue();
    }
  };
}

Libraries like idb or Dexie.js make IndexedDB much easier with promises.

Offline websites: service workers (replacing AppCache)

AppCache used a manifest file to list pages to keep offline, but it was unreliable and has been removed from browsers. Today, a service worker handles offline support. It's a script that intercepts network requests and serves cached files when there's no connection.

// sw.js
const CACHE = 'site-v1';
const FILES = ['/', '/about.html', '/style.css', '/main.js', '/offline.html'];

self.addEventListener('install', (e) => {
  e.waitUntil(caches.open(CACHE).then((cache) => cache.addAll(FILES)));
});

self.addEventListener('fetch', (e) => {
  e.respondWith(
    caches.match(e.request).then((cached) =>
      cached || fetch(e.request).catch(() => caches.match('/offline.html'))
    )
  );
});

Register it from your page:

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js');
}

Tools like Google's Workbox simplify service worker setup. Combined with a web app manifest, this lets you build a progressive web app (PWA).


Need help building a fast, offline-capable web app? I.T. For Less can help — contact us.

Development