Browser storage patterns

From farming-labs/fm-skills · View interactive version

Browser storage patterns is an AI agent skill from the GitHub repository farming-labs/fm-skills. Explains browser storage patterns including cookies, localStorage, sessionStorage, and when to use each. Use when deciding how to persist client-side data, handle auth/session concerns, or choose safe storage patterns for preferences, drafts, and browser state.

How to use this skill

Skill file

Browser Storage Patterns

Overview

Browser storage is for data that must survive beyond a single render or request.

Common questions:

Choosing the wrong storage mechanism creates security, UX, and correctness problems quickly.

Storage Options

| Storage | Best For | Scope | Sent to Server | Persistence |

|---------|----------|-------|----------------|-------------|

| Cookies | Sessions, server-readable flags | Per browser/profile | Yes | Configurable |

| localStorage | Preferences, low-sensitivity persistence | Per origin | No | Until cleared |

| sessionStorage | Tab-scoped drafts, flow state | Per tab | No | Until tab closes |

| IndexedDB | Larger structured offline data | Per origin | No | Until cleared |

For most app decisions, the key choice is between cookies, localStorage, and sessionStorage.

Decision Guide

Use this rough mapping:

If the data is sensitive, do not default to localStorage.

Cookies

Cookies are small key-value pairs stored by the browser and attached to matching HTTP requests.

They are useful when:

Good Cookie Use Cases

Important Cookie Attributes

Security Rule

For auth sessions, HttpOnly cookies are usually safer than storing tokens in localStorage.

localStorage

localStorage is a synchronous browser key-value store that persists until cleared.

It is useful when:

Good localStorage Use Cases

Example

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

Caveats

sessionStorage

sessionStorage is similar to localStorage, but scoped to one browser tab.

It is useful when:

Good sessionStorage Use Cases

Example

sessionStorage.setItem('draft-id', draftId);
const currentDraft = sessionStorage.getItem('draft-id');

IndexedDB

IndexedDB is more powerful than localStorage, but heavier to work with.

Use it when:

Do not reach for IndexedDB if a small preference key is all you need.

Common Usage Patterns

Pattern 1: Theme and UI Preferences

Store low-risk, user-owned preferences in localStorage.

const savedTheme = localStorage.getItem('theme') || 'system';

Good examples:

Pattern 2: Session Auth with Cookies

Store auth session state in secure cookies when the server must participate in auth decisions.

Good examples:

This works well because the server can check auth before sending protected content.

Pattern 3: Tab-Scoped Draft State

Use sessionStorage for workflows that should survive refresh but not be shared across tabs.

Good examples:

Pattern 4: Client-Only Dismissal State

Use localStorage for UI that should stay dismissed across visits.

const dismissed = localStorage.getItem('promo-dismissed') === 'true';

Examples:

Pattern 5: Hybrid Session + Preference Model

A common production setup:

This keeps sensitive state server-readable while leaving browser-only UI preferences in client storage.

Security Patterns

Storage decisions are often security decisions.

Safer Defaults

Why localStorage Is Risky for Sensitive Tokens

If malicious JavaScript runs in the page, it can usually read localStorage.

That means storing highly sensitive tokens there increases the blast radius of XSS.

SSR and Hydration Caveats

Browser storage APIs do not exist during server render.

This means code like this will break in SSR environments:

const theme = localStorage.getItem('theme');

Safer pattern:

let theme = 'light';

if (typeof window !== 'undefined') {
  theme = localStorage.getItem('theme') || 'light';
}

For values needed before paint, consider whether the server should read a cookie instead.

Expiry and Staleness

localStorage does not expire by itself.

If data should go stale, add your own timestamp.

localStorage.setItem('notice', JSON.stringify({
  value: true,
  expiresAt: Date.now() + 86400000,
}));

This is useful for:

Cross-Tab Behavior

localStorage is shared across tabs for the same origin.

That is useful for:

But it can also be surprising if a workflow is meant to stay isolated.

Use sessionStorage when tab isolation matters more than shared persistence.

Serialization Patterns

Browser storage stores strings.

Use JSON carefully:

localStorage.setItem('prefs', JSON.stringify({ theme: 'dark' }));
const prefs = JSON.parse(localStorage.getItem('prefs') || '{}');

Guard against:

If the schema evolves, add versioning or migration logic for important stored data.

Failure Cases

Storage access is not always guaranteed.

Possible issues:

Good pattern:

Common Anti-Patterns

| Anti-Pattern | Why It Hurts |

|-------------|--------------|

| Storing auth secrets in localStorage by default | Increases XSS risk |

| Using cookies for large client-only state | Bloats requests |

| Using localStorage for tab-scoped flows | State leaks across tabs |

| Reading browser storage during SSR without guards | Breaks server render |

| Storing complex app data with no versioning | Upgrade and parse issues |

Rules of Thumb

Related Skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.