{"success":true,"name":"after-a-mutation-refresh-every-view-that-depends-on-it-stale","description":"Creating/editing a record while lists, counters, or other screens still show old data","category":"ai-brimind","path":"ai-brimind/after-a-mutation-refresh-every-view-that-depends-on-it-stale/SKILL.md","file":"SKILL.md","content":"---\nname: after-a-mutation-refresh-every-view-that-depends-on-it-stale\ndescription: \"Creating/editing a record while lists, counters, or other screens still show old data\"\n---\n\n# After a Mutation, Refresh Every View That Depends on It: Stale Lists Are Broken Promises\n\n## When to use\nThe frontend performs a mutation (create, update, delete) but other views showing that data — lists, dashboards, badges, detail pages — keep displaying their previously fetched copy.\n\n## Example request\nAdding a member succeeds, navigating back shows the list WITHOUT them → the list component still holds its old fetch; the backend changed, the frontend's copy did not, and to the user 'the add silently failed'. Using a data-fetching library (React Query/SWR) → mutation success calls invalidateQueries for the affected keys; declaring 'this write stales those reads' is the library's whole reason to exist — use its mechanism, not manual refetch spaghetti. The same data appearing in a list, a counter badge, and a dashboard → your mutation must know its DEPENDENTS, which is your blast-radius discipline running inside the frontend. Hand-rolled fetching in useState → the honest minimum is refetch-on-return or updating local state from the mutation's response; picking one deliberately beats hoping navigation remounts things.\n\n## Instructions\nAct as a frontend developer who treats every fetched dataset as a photograph — true the instant it was taken, aging every second after — and every mutation as the event that turns some photographs into lies; the job is to know, for each write, WHICH photographs just expired, and to refresh them through one deliberate mechanism rather than hoping remounts and luck keep the UI honest. For every mutation you wire, do this: (1) List the dependents before writing the success handler: which queries/views show data this write changed? The list it belongs to, the counters that include it, the detail view of a parent, the dashboard aggregate — this is your blast-radius discipline applied to client state, and like there, memory says two and reality says five: check the screens. (2) Use the caching layer's invalidation as the mechanism if the project has one (React Query/SWR): key queries hierarchically (['members', clubId], ['members', clubId, memberId]) so one invalidateQueries(['members', clubId]) expires the family; on mutation success, invalidate the dependent keys — this is declaring the dependency edge ONCE instead of sprinkling refetch calls, and it is the tool's core purpose, per your reuse discipline. Choose consciously between invalidate-and-refetch (simple, always true) and optimistic updates (snappy, but you must handle rollback on failure — a deliberate upgrade, not a default). (3) Without a caching library, pick the honest fallback and standardize it: mutations return the fresh record (your response-shaping discipline: return what the client needs to update itself), and the calling screen either updates its local state from that response or triggers a refetch — one convention across the app, not a different improvisation per screen. (4) Mind the cross-boundary dependents your instinct misses: a server-component-rendered list in Next.js needs router.refresh() after a client-side mutation — invalidating client caches does nothing for HTML the server rendered; the server/client boundary from your rendering discipline is also a cache boundary. (5) Verify with the round-trip test: perform each mutation, then visit EVERY dependent view without a manual reload — each must show the new truth. Only call the mutation done when its success handler names its dependents explicitly — a write whose dependents update 'because the page probably remounts' is not synchronized, it is gambling with the user's trust that saving means saved.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #247\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}