
Client Data Fetching
Guides client API hooks, cache invalidation, async errors and persistent caches.
Install
npx skills add lobehub/lobehub --skill data-fetching-architecture -g -a claude-code -y- Repository
- lobehub/lobehub83.7k
- Category
- Engineering
- Updated
- 3d ago
- License
- See repository
- Context cost
- Mediumabout 1,750 tokens
- Runs scripts
- No, instructions only
What it does
Use for client APIs, SWR hooks, cache invalidation, async errors, useEffect migration, home first paint and persistent caches.
LobeHub Data Fetching Architecture
Component → Store useFetchXxx hook → Service → lambdaClient
← SWR request state + store data ← response
Layer boundaries
- Services in
src/services/own API calls:.query()for reads,.mutate()for writes. Export a service instance per domain; components and stores do not calllambdaClientdirectly. - Store read hooks use
useClientDataSWRand return its SWR response, includingerrorandmutate. Sync successful results into the store through the wrapper's supported callback (onSuccess, oronDatafor the sync wrapper). - Server lists and details that paint from a local copy go through
@/libs/replicainstead: the store'suseFetchXxxreturnsslice.useSync(params)(flags only) and the engine writes the store. See the Replica section of zustand. - Components call these hooks and read store data through selectors. Do not fetch in
useEffector duplicate server data in componentuseState. - Use
useFetchXxxfor read hooks andrefreshXxxfor cache invalidation. - For list/detail types, maps, reducers, and shared type sources, use Zustand data structures. For store layout and action classes, use zustand.
Home First Paint and Persistent Caches
When changing home/sidebar first paint or persisted display data, read
references/home-first-paint.md. It covers
avoiding flicker, reusing persistence, and the user displaySnapshot.ts boundary.
Cache keys and refresh
Reuse the domain key factory in src/libs/swr/keys.ts; define a shared key if the
domain has none. Include every parameter that changes the response: entity or parent
id, filters, sort, and pagination. Read and refresh must construct the same key.
