Client State Topology: Ephemeral, URL, Server Cache & Persistent
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.
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.
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
};Deep-linking loses sortBy, stale users linger without TTL, and localStorage blocks INP.
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);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.
- Ephemeral UI state: Hover flags, modal open toggles, and draft input text live in component memory and unmount with the DOM subtree.
- Navigational URL state: Filters, sort order, and pagination serialize into
URLSearchParamsvia URL as State Store so reloads and shared links restore the exact view. - Server cache state: Remote records belong to a dedicated Server State Cache Machine that tracks
staleTime, deduplicates in-flight fetches, and invalidates on mutation. - Durable client state: Offline drafts and user preferences write asynchronously to IndexedDB to survive tab termination.
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
isFetchingflags 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
URLSearchParamsand IndexedDB sync engines as explicit topology layers.
Production Pitfalls
- Back button resets search filters: Storing pagination or filter facets in memory instead of
URLSearchParamswipes 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 URL state: Never serialize high-frequency coordinates (drag offsets, canvas pan) or sensitive PII tokens into
history.replaceState(); Blink throttlesreplaceStateafter 100 calls per 30 seconds and URLs leak into browser history andRefererheaders.
Connections & Logical Next Steps
Shareable filters, pagination, and active tabs belong in URLSearchParams rather than in-memory stores.
Remote entities require staleness tracking, deduplication, and background revalidation instead of manual store slices.