State & ArchitectureBaseline · Widely

Client State Topology: Ephemeral, URL, Server Cache & Persistent

Mental Model

Dumping every variable into one global store breaks browser Back navigation, leaks stale server responses, and triggers tree-wide re-renders. Partitioning client state across four distinct owners (ephemeral memory, URL search params, server cache, and IndexedDB) keeps each source of truth synchronized with its lifecycle.

Interactive Simulator

Monolithic Global Store vs Four-Tier State Topology

Compare coupling all state in one global store against routing each state kind to its native owner.

Lines of Code
Before
5
After
4
Reflows / Browser Cost
Before
Global subscribers notified on modal toggle
After
Scoped updates per tier
BeforeMonolithic Store (Single Global Slice)
const store = {
  users: [], isFetchingUsers: false, // stale server cache
  sortBy: 'name',                    // breaks Back button
  isFilterModalOpen: false,          // global re-render on toggle
  unsavedNote: localStorage.getItem('note') // sync main-thread I/O
};
Lines: 5Reflows: Global subscribers notified on modal toggleDependencies: Manual sync between URL, cache, and storage

Deep-linking loses sortBy, stale users linger without TTL, and localStorage blocks INP.

After (Native)Four-Tier Topology (Explicit Owners)
const sortBy = new URLSearchParams(location.search).get('sort');
const { data: users } = useServerCache('/api/users', { staleMs: 30_000 });
const [modalOpen, setModalOpen] = useState(false);
await idbStore.put('drafts', unsavedNote);
Lines: 4Reflows: Scoped updates per tierDependencies: Native URL, IDB, and cache invalidation

Back button restores sortBy, cache revalidates stale users, and modal toggles stay local.

Under the Hood

Every client state variable has an authoritative lifecycle and a serialization boundary. Assigning state to the wrong tier forces manual synchronization code that drifts out of sync across renders and navigations.

  1. Ephemeral UI state: Hover flags, modal open toggles, and draft input text live in component memory and unmount with the DOM subtree.
  2. Navigational URL state: Filters, sort order, and pagination serialize into URLSearchParams via URL as State Store so reloads and shared links restore the exact view.
  3. Server cache state: Remote records belong to a dedicated Server State Cache Machine that tracks staleTime, deduplicates in-flight fetches, and invalidates on mutation.
  4. Durable client state: Offline drafts and user preferences write asynchronously to IndexedDB to survive tab termination.
TS
type Topology<T> = {
  ephemeral: { dropdownOpen: boolean };
  url: URLSearchParams; // ?page=2&sort=asc
  serverCache: Map<string, { data: T; fetchedAt: number }>;
  persistent: IDBDatabase; // offline drafts & auth metadata
};

function selectOwner(isRemote: boolean, isShareable: boolean, isDurable: boolean) {
  if (isRemote) return 'serverCache';
  if (isShareable) return 'url';
  if (isDurable) return 'persistent';
  return 'ephemeral';
}

Problem Genealogy

  • 2015–2018 (Monolithic Redux Stores): Teams normalized API payloads, modal booleans, and filter strings into one global object, shipping 40+ KB of boilerplate and forcing manual isFetching flags for every endpoint.
  • 2019–2022 (Server-Cache Split): Dedicated query caches removed ~70% of client store slices by separating remote snapshots from local UI state.
  • 2023+ (URL & Local-First Convergence): Frameworks standardized URLSearchParams and IndexedDB sync engines as explicit topology layers.

Production Pitfalls

Production Pitfall
  • Back button resets search filters: Storing pagination or filter facets in memory instead of URLSearchParams wipes user context on history pop or link sharing.
  • Mirroring server cache into local state: Copying fetched rows into component state on mount disconnects the view from background revalidation and serves stale mutations.
When Not to Use
  • When not to use URL state: Never serialize high-frequency coordinates (drag offsets, canvas pan) or sensitive PII tokens into history.replaceState(); Blink throttles replaceState after 100 calls per 30 seconds and URLs leak into browser history and Referer headers.

Connections & Logical Next Steps

State & ArchitectureClient State Topology: Ephemeral, URL, Server Cache & Persistent
What This Unlocks
Leaf concept