Agent Wranglers

Client and server state Required

Key cached data by every input

Build every cache key from all the inputs that change the answer, including the account, the signed-in user, the filters, the sort and the page.

How to do it

Put the account or tenant first, so one call can drop all of its data. Then add the resource and each parameter. Pass the same values to the request and to the key from one object, so they can't drift apart. Clear the whole cache on sign-out and on switching accounts. The same rule holds for server-side caches, as caches and copies explains.

Example

export const orderKeys = {
  all: (accountId: string) => ['accounts', accountId, 'orders'] as const,
  list: (accountId: string, filters: OrderFilters) => [...orderKeys.all(accountId), 'list', filters] as const,
};

export function useOrders(accountId: string, filters: OrderFilters) {
  return useQuery({ queryKey: orderKeys.list(accountId, filters), queryFn: () => api.listOrders(accountId, filters) });
}

export function onAccountSwitch(queryClient: QueryClient) {
  queryClient.clear();
}
def orders_cache_key(account_id: str, filters: OrderFilters) -> str:
    return ":".join(["orders", account_id, filters.status or "any", filters.sort, filters.cursor or "first"])

How to check

A test signs in to account A, loads orders, switches to account B and loads orders. No request answer or rendered row from account A appears.

For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers app interface playbook: https://agentwranglers.se/playbook/app-ui/client-and-server-state/complete-cache-keys.