Skip to main content

Cache Key Registry

Objective

Create a single, typo-safe place for root cache key names and a shared factory for all TanStack Query keys.

Files

  • lib/cache-keys.ts
  • lib/query-keys.ts

Rules

Root key names

  • every root key name must come from CACHE_KEYS
  • keys must be uppercase in the registry
  • no hard-coded root key strings outside lib/cache-keys.ts and lib/query-keys.ts
Example:

Query key composition

  • all query key arrays must be created in lib/query-keys.ts
  • every user-scoped key must include userId at index [1]
  • every domain should expose an .all key for prefix invalidation
Example:

Naming Guidelines

  • use singular domain groups in the factory object when it reads well, but keep root strings aligned with existing cache language
  • prefer list, detail, summary, stats, byId, byDate, unified as stable suffix conventions
  • avoid inventing multiple suffix names for the same pattern

Migration Steps

  1. Create CACHE_KEYS with every root query key currently in use.
  2. Build globalKeys and userKeys(userId).
  3. Replace local factories such as createQueryKeys.
  4. Replace inline key arrays with factory helpers.
  5. Leave no orphaned root key string arrays behind.

Review Checklist

  • does the domain have an .all key?
  • is userId at index [1] for user-scoped data?
  • does the new factory preserve existing cache segmentation where needed?
  • are there any inline arrays left in the touched files?