PresentationInteractive, narrated presentation of the section content.

ContentDetailed description of the section content.

Exploring Web Storage Beyond Cookies

Motivation: Why Storage Matters

Hey everyone, welcome back! Last time, we tackled cookies—those little helpers that keep our Blog application remembering users. But cookies aren’t the only game in town! Today, we’re exploring other storage options like local storage and session storage, plus a quick peek at IndexedDB. Why care? Because choosing the right storage can make or break how our app feels—whether it’s fast, secure, or user-friendly.

Imagine our Blog application: Users read posts, admins edit them, and maybe someone wants to save a draft offline. Cookies handled logins, but what if a user wants to save preferences forever or a draft for just one tab? That’s where these other options shine. A misconception to bust: Some think cookies are the only way to store data on the client—wrong! Let’s see how local storage, session storage, and more stack up, using our Blog app as our playground.


Section 1: Local Storage – The Long-Term Keeper

What is it? Local storage is a browser feature that saves data as key-value pairs, like cookies but with more space and no automatic server trips. It sticks around until you delete it—no expiration!

How it works: You use JavaScript’s localStorage object. In our Blog app, let’s save a user’s post filter preference:

// Save preference
localStorage.setItem('prefs', 'tag:tech');
// Read it later
const prefs = localStorage.getItem('prefs'); // 'tag:tech'

Blog App Example: A user sets prefs=tag:tech to filter posts. They close the browser, come back a week later, and it’s still there—local storage remembers!

Pros:

  • Big capacity: 5-10MB (way more than cookies’ 4KB).
  • Persistent: Stays until cleared—great for long-term data.
  • Simple: No server setup, just JS.

Cons:

  • No security: Anyone with access to the browser can read it (no HttpOnly equivalent).
  • Client-only: Doesn’t travel to the server like cookies.
  • Sync issues: Doesn’t sync across devices.

Section 2: Session Storage – The Tab-Specific Temp

What is it? Session storage is like local storage’s short-term cousin. It’s also key-value pairs, but it only lasts for the current browser tab’s session—close the tab, and it’s gone.

How it works: Use sessionStorage in JS. For our Blog app, let’s store a temporary post draft:

// Save draft in one tab
sessionStorage.setItem('draft', 'My awesome post...');
// Read it
const draft = sessionStorage.getItem('draft'); // 'My awesome post...'

Blog App Example: An admin starts a draft in one tab. It’s saved in sessionStorage. They open a new tab—poof, it’s not there! Close the original tab, and it’s gone for good.

Pros:

  • Tab isolation: Each tab gets its own storage—perfect for temporary, tab-specific data.
  • Big capacity: Same 5-10MB as local storage.
  • Simple: No server needed.

Cons:

  • Short-lived: Dies with the tab—not for long-term use.
  • No server sync: Like local storage, it’s client-only.
  • No security: Still readable by JS.

Section 3: IndexedDB – The Powerhouse

What is it? IndexedDB is a full-on database in the browser. Unlike key-value pairs, it stores structured data (objects, arrays) and handles big datasets.

How it works: It’s more complex, using promises. Here’s a simplified Blog app example to store multiple drafts:

// Open database
const dbPromise = indexedDB.open('BlogDrafts', 1);
dbPromise.onupgradeneeded = (event) => {
  event.target.result.createObjectStore('drafts', { keyPath: 'id' });
};
// Save a draft
dbPromise.onsuccess = (event) => {
  const db = event.target.result;
  const tx = db.transaction('drafts', 'readwrite');
  tx.objectStore('drafts').add({ id: 1, text: 'Draft 1...' });
};

Blog App Example: An admin saves multiple drafts. IndexedDB stores them with IDs, searchable later—even offline!

Pros:

  • Huge capacity: Tens of MBs, depending on the browser.
  • Structured data: Great for complex apps (e.g., offline drafts).
  • Powerful: Searchable, like a mini SQL database.

Cons:

  • Complex: Steeper learning curve than local/session storage.
  • Client-only: No server integration out of the box.
  • No built-in security: Still JS-accessible.

Section 4: Comparing to Cookies

Let’s pit these against cookies in our Blog app:

Cookies (Recap):

  • Use Case: Guest tracking (visitor_id), admin sessions (session_id).
  • How: Set via cookies().set() (Next.js) or document.cookie, sent to server automatically.
  • Size: 4KB max.
  • Lifespan: Set by maxAge or session-based.

Comparison Table:

FeatureCookiesLocal StorageSession StorageIndexedDB
Size4KB5-10MB5-10MBTens of MBs
LifespanCustom/SetForeverTab closesForever
Server AccessYes (HTTP)NoNoNo
SecurityHttpOnly, etc.NoneNoneNone
Ease of UseMediumEasyEasyHard
Blog App UseLogin, prefsLong-term prefsDrafts per tabOffline drafts

Blog App Scenarios:

  • Cookies: session_id for admin login—secure, server-synced.
  • Local Storage: prefs=tag:tech—persistent across sessions.
  • Session Storage: Temporary draft—tab-specific, no clutter.
  • IndexedDB: Multiple drafts—complex, offline-ready.

Benefits:

  • Cookies: Secure, server-friendly (auth foundation).
  • Local: Big, persistent (user convenience).
  • Session: Isolated, temporary (clean UX).
  • IndexedDB: Scalable, powerful (advanced features).

Limitations:

  • Cookies: Tiny size, security setup needed.
  • Local/Session: No server sync, no security.
  • IndexedDB: Complexity, still client-only.

Conclusion: Key Takeaways

Today, we expanded our Blog app’s storage toolkit! Cookies track logins, local storage saves prefs forever, session storage handles tab-specific drafts, and IndexedDB powers big offline data—all with trade-offs.

Key Points:

  • Choose based on need: size, lifespan, server sync.
  • Cookies excel for auth; others shine for client-side data.
  • Security matters—cookies lead here with HttpOnly.

Best Practices:

  • Use cookies for server-handled state (logins).
  • Local for persistent UX, session for temp, IndexedDB for scale.
  • Avoid sensitive data in unsecured storage.

Pitfalls:

  • Don’t overload cookies—use local/IndexedDB for big data.
  • Don’t expect server sync from non-cookie options.

No hints available.

Discuss with OthersAsk questions, share your thoughts, and discuss with other learners.

Loading discussion ...
Setup
React Fundamentals
Next.js
Advanced React
Databases
React Hooks
Authentication and Authorisation
APIs
CI/CD and DevOps
Testing React
Advanced Topics