State & Architecture

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

Eliminates stale data, broken back-button navigation, and redundant re-renders by assigning every piece of UI state to its canonical owner: ephemeral memory, URL search params, a server cache, or IndexedDB.

About this page
READ
2 min
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.

Monolithic Global Store vs Four-Tier State Topology

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#

  • 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.

Where this connects

Prerequisites

State & Architecture

URL as a State Store

Shareable filters, pagination, and active tabs belong in URLSearchParams rather than in-memory stores.