01 · Before and afterThe pattern
// before: scope and freshness resolved after the request returns
const doc = await api.get(id);
cache.replace(id, doc);
// after: capture both before awaiting, re-check after
const scope = cache.scope.get();
const seen = writes.get(id) ?? 0;
const doc = await api.get(id);
if (scope !== cache.scope.get() || (writes.get(id) ?? 0) !== seen) return;
cache.replace(id, doc, scope);02 · Run it on your appThe check
If a user switches account while a request is in flight, can that response end up in the new account's local cache?
If the honest answer is no or not sure, this is worth a look before it reaches production.