Browser Storage Types For Web Applications

 In the browser, there are several types of storage mechanisms available for frontend applications. Each serves different purposes and comes with its own capabilitieslifespans, and security considerations.


🗂️ Browser Storage Types (Overview)

Storage TypeSize LimitPersistent?Accessible FromAuto Expiry
localStorage~5–10 MB✅ YesAny script on the same origin❌ No (manual clear only)
sessionStorage~5 MB❌ NoSame tab only✅ When tab closes
cookies~4 KB✅ OptionalSent with each HTTP request✅ Optional (expiry based)
IndexedDB~50–100 MB✅ YesSame origin❌ No
WebSQL (Deprecated)

✅ 1. localStorage

🟢 Use When:

  • You need to persist non-sensitive user preferences or cached data.
  • Data should remain after browser/tab is closed.

🔧 Example:

// Save user theme preference
localStorage.setItem('theme', 'dark');

// Retrieve it
const theme = localStorage.getItem('theme');

// Remove it
localStorage.removeItem('theme');

⚠️ Security Warning:

  • ❌ Not encrypted.
  • ❌ Accessible via JavaScript — vulnerable to XSS attacks.
  • ❌ Never store tokens, passwords, or PII.

✅ 2. sessionStorage

🟢 Use When:

  • Data is only needed for a single tab session (e.g., form data, temp flags).
  • Good for multi-tab isolation.

🔧 Example:

sessionStorage.setItem('draftNote', 'Hello world!');
const note = sessionStorage.getItem('draftNote');

⚠️ Like localStoragenot secure for sensitive data.


✅ 3. Cookies

🟢 Use When:

  • You need to persist small pieces of data (e.g., login tokens).
  • You want to send data to the server automatically with every request.

🔧 Example:

document.cookie = "username=JohnDoe; path=/; max-age=86400"; // 1 day

⚠️ Be careful:

  • Sent with every HTTP request — performance hit if overused.
  • Secure tokens must use:

    • Secure (HTTPS only)
    • HttpOnly (not accessible via JS)
    • SameSite=Strict (CSRF protection)

✅ 4. IndexedDB

🟢 Use When:

  • You need to store large structured data, like:

    • Offline web apps
    • Caching API responses
    • Storing media, files, or complex objects

🔧 Example (Using idb wrapper library):

import { openDB } from 'idb';

const db = await openDB('notes-db', 1, {
  upgrade(db) {
    db.createObjectStore('notes');
  }
});

await db.put('notes', 'Buy milk', 'note1');
const note = await db.get('notes', 'note1');

⚠️ Safe from XSS? Not by default. Same precautions as others apply.


🔒 Storing Sensitive Data – What NOT to Do

❌ DON'T StoreWhere
Access tokens / JWTslocalStoragesessionStorage
Passwords or PIIAny client-side storage
CSRF tokensCookies with HttpOnly flag preferred

🛡️ Best Practices for Sensitive Data

✅ Use HttpOnly Cookies for tokens:

Set-Cookie: token=abc123; HttpOnly; Secure; SameSite=Strict

✅ Use HTTPS always ✅ Sanitize all user input to prevent XSS ✅ Regularly clear unused storage ✅ Prefer IndexedDB for large/structured data with service workers


🎯 Real-Life Use Case Scenarios

FeatureStorageWhy
Remember dark/light themelocalStoragePersistent across tabs
Retain form state in tabsessionStorageCleared on tab close
Offline caching of app dataIndexedDBCan handle large structured data
Auth token storage (securely)HttpOnly CookieNot accessible from JS
Remember last visited pagelocalStorageRetained between sessions

Vikash Chauhan

C# & .NET experienced Software Engineer with a demonstrated history of working in the computer software industry.

Post a Comment

Previous Post Next Post

Contact Form